Home java Sort Descending

Sort Descending

Author

Date

Category

Just make a reservation, I am new to programming and yet knowledge of the theory of algorithms can not boast.
Today solved the problem of sorting an array consisting of the integral elements which should regulate in descending order. Frankly, it was hard to solve this problem, go to the principle and googled the solution to this problem. Something I wrote was very close, but still something was missing. I decided to google and found a solution to this problem, was satisfied with the fact that my code was similar to the decision. Here is the code:

for (int j = 0; j & lt; intArray.length; j ++)
    for (int k = 0; k & lt; intArray.length - 1; k ++) {
      if (intArray [k] & lt; intArray [k + 1]) {
        temp = intArray [k];
        intArray [k] = intArray [k + 1];
        intArray [k + 1] = temp;
      }
    }

Let us examine an example. Suppose we have an array of integer elements (3, 2, 5, 1). After sorting, we obtain (5, 3, 2, 1).
Step by step it will look like this:

j = 0; k = 0; (3 & lt; 2) - & gt; false - & gt; March 2 May 1
    k = 1; (2 & lt; 5) - & gt; true - & gt; May 3 January 2
    k = 2; (2 & lt; 1) - & gt; false - & gt; May 3 January 2
j = 1; k = 0; (3 & lt; 5) - & gt; true - & gt; 5 3 2 1
    k = 1; (3 & lt; 2) - & gt; false - & gt; 5 3 2 1
    k = 2; (2 & lt; 1) - & gt; false - & gt; 5 3 2 1

After the second iteration of the outer loop, we already have a ready answer, it is sufficient for this example, but not for all. The question is why the outer loop must be controlled precisely this condition (j & lt; intArray.length)? How do you know that for complete success, it is necessary to such a condition? Just want to understand this algorithm inside … Thank you for your attention.


Answer 1, Authority 100%

sorting method that you used the method called bubble sort. It got its name because (with decreasing sorting) is the smallest element occupies the extreme right position in the array by sequentially comparing adjacent elements of the array and rearranging them if necessary.

That is, if you have an array

{1, 2, 3, 4}

after the first pass through the array

{1, 2, 3, 4}
 ^^^
{2, 1, 3, 4}
  ^^^
{2, 3, 1, 4}
    ^^^
{2, 3, 4, 1}
     ^^^

in the last position of the array will be the number 1.

What is the smallest number as it is pushed upward, or as air bubble comes up.

Then you do not need to pass the entire set, as already in the last position of the array is the smallest element.

So you want to apply to sort the array with the number of elements is one less than the original array, that is, the elements

{2, 3, 4}

Again 2 “floats” in the most extreme right position, and you get

{3, 4, 2, 1}

etc.

If the array is already sorted initially, there is no need to make several passes through the array. How to determine the array is sorted or not?
Very simple! if there was no permutations when passing through the array, the array is already sorted.

All of these considerations lead to the following code

int last = intArray.length;
for (boolean sorted = last == 0;! sorted; --last)
{
  sorted = true;
  for (int i = 1; i & lt; last; ++ i)
  {
    if (intArray [i-1] & lt; intArray [i])
    {
      sorted = false;
      int tmp = intArray [i-1];
      intArray [i-1] = intArray [i];
      intArray [i] = tmp;
    }
  }
}

As you can see in the code

  • the upper limit of the array is constantly decreases as
    The smallest element in the under-array occupies an extreme right position

  • Sort stops as soon as it is discovered that permutations
    There were no neighboring elements, that is, the array is already
    sorted.


Answer 2, Authority 80%

Your implementation is not optimal. Please note that the variable J is not used anywhere in the cycle.
It should be about this:

for (int j = 0; j & lt; intarray.length - 1; j ++)
{
  For (int k = j + 1; k & lt; intarray.length; k ++)
  {
    if (Intarray [j] & lt; intarray [k])
    {
      int temp = intarray [k];
      INTARRAY [K] = INTARRAY [J];
      INTARRAY [J] = TEMP;
    }
  }
}

And the meaning of bubble sorting is: you need to run all the pairs of array elements and, if necessary, change them in places. Therefore, j runs 0 ... N-1 , and k – elements j + 1 ... N , where n – the index of the last element.


Answer 3, Authority 20%

Sorting an array descending:

int temp;
  For (int i = 0; i & lt; array.length; I ++) {
    for (int k = i + 1; k & lt; array.length; k ++) {
      if (array [i] & lt; array [k]) {
        TEMP = Array [i];
        array [i] = array [k];
        array [k] = temp;
      }
    }
  }

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions