Int [] array (sort from lowest to highest)

So I’m not sure why it becomes so difficult for me, but I need to sort from high to low and low to high.

For high and low, I have:

int a, b; int temp; int sortTheNumbers = len - 1; for (a = 0; a < sortTheNumbers; ++a) { for (b = 0; b < sortTheNumbers; ++b) { if (array[b] < array[b + 1]) { temp = array[b]; array[b] = array[b + 1]; array[b + 1] = temp; } } } 

However, I can’t make it work for the rest of my life in the reverse order (from low to high), I thought that the logic goes through and it always returns 0 for all values. Any help appreciated!

The bigger the picture, the more I have a JTable with 4 columns, each column with numbers of numbers, names or dates. I need to be able to sort them back and forth.

Thanks!

+8
java sorting arrays
source share
10 answers

If you don’t think that using the already available sorting and autoboxing features is tricking:

 Integer[] arr = { 12, 67, 1, 34, 9, 78, 6, 31 }; Arrays.sort(arr, new Comparator<Integer>() { @Override public int compare(Integer x, Integer y) { return x - y; } }); System.out.println("low to high:" + Arrays.toString(arr)); 

Print low to high:[1, 6, 9, 12, 31, 34, 67, 78]

if you need high to low xy to yx in the comparator

+18
source share

You never visit the last element of an array.

In addition, you should know that bubble sort is pretty inefficient, and you can just use Arrays.sort() .

+3
source share
  public class sorting { public static void main(String arg[])throws Exception{ int j[]={1,28,3,4,2}; //declaring array with disordered values for(int s=0;s<=j.length-1;s++){ for(int k=0;k<=j.length-2;k++){ if(j[k]>j[k+1]){ //comparing array values int temp=0; temp=j[k]; //storing value of array in temp variable j[k]=j[k+1]; //swaping values j[k+1]=temp; //now storing temp value in array } //end if block } // end inner loop } //end outer loop for(int s=0;s<=j.length-1;s++){ System.out.println(j[s]); //retrieving values of array in ascending order } } } 
+3
source share

The only thing you need to do to change the sort order is change

 if (array[b] < array[b + 1]) 

to

 if (array[b] > array[b + 1]) 

Although, as others have noted, this is very inefficient! :-)

+1
source share

You just need to write the line one Arrays.sort(arr) for low to high for Java 8.

Arrays.sort(arr, Collections.reverseOrder()) for high to low

+1
source share

In java8 you can do something like this:

 temp.stream() .sorted((e1, e2) -> Integer.compare(e2, e1)) .forEach(e -> System.out.println(e)); 
+1
source share

You need a more efficient look. like mergesort. try www.geekviewpoint.com and go to sort

0
source share

If you just want to sort the int array: use quicksort ... This is not a lot of code, but N * lgN in avarage or N ^ 2 in the worst case. To sort multiple data, use Java Compare (as above) or a stable sorting algorithm

 static void quicksort(int[] a,int l, int r){ if(r <= l) return; int pivot = partition(a,l,r); //Improvement, sort the smallest part first if((pivot-l) < (r-pivot)){ quicksort(a,l,pivot-1); quicksort(a,pivot+1,r); }else{ quicksort(a,pivot+1,r); quicksort(a,l,pivot-1); } } static int partition(int[] a,int l,int r){ int i = l-1; int j = r; int v = a[r]; while(true){ while(less(a[++i],v)); //-> until bigger while((less(v,a[--j]) && (j != i))); //-> until smaller and not end if(i >= j){ break; } exch(a,i,j); } exch(a,i,r); return i; } 
0
source share

You can try with bubble sorting: Example shown below

 int[] numbers = { 4, 7, 20, 2, 56 }; int temp; for (int i = 0; i < numbers.length; i++) { for(int j = 0; j < numbers.length; j++) { if(numbers[i] > numbers[j + 1]) { temp = numbers [j + 1]; numbers [j + 1]= numbers [i]; numbers [i] = temp; } } } for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i].toString()); } 
-one
source share

Let me know if this works:

 public class prog1 { public static void main (String args[]){ int a[] = {1,22,5,16,7,9,12,16,18,30}; for(int b=0; b<=a.length;b++){ for(int c=0; c<=a.length-2;c++){ if(a[c]>a[c+1]){ int temp=0; temp=a[c]; a[c]=a[c+1]; a[c+1]=temp; } } } for(int b=0;b<a.length;b++){ System.out.println(a[b]); } } } 
-2
source share

All Articles