[英]int[] array (sort lowest to highest)
So I am not sure why this is becoming so hard for me, but I need to sort high to low and low to high. 所以我不确定为什么这对我来说变得如此困难,但我需要从高到低,从低到高排序。
For high to 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 for the life of me get it to work in reverse (low to high), I have thought the logic through and it always returns 0's for all the values. 但是,我不能为我的生活让它反向工作(从低到高),我已经考虑过逻辑,它总是为所有值返回0。 Any help appreciated!
任何帮助赞赏!
The bigger picture is that I have a JTable with 4 columns, each column with entries of numbers, names, or dates. 更大的图景是我有一个4列的JTable,每列都有数字,名称或日期的条目。 I need to be able to sort those back and forth.
我需要能够来回排序。
Thanks! 谢谢!
Unless you think using already available sort functions and autoboxing is cheating: 除非您认为使用已经可用的排序功能并且自动装箱是作弊:
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));
Prints low to high:[1, 6, 9, 12, 31, 34, 67, 78]
从
low to high:[1, 6, 9, 12, 31, 34, 67, 78]
打印low to high:[1, 6, 9, 12, 31, 34, 67, 78]
if you need high to low change xy
to yx
in the comparator 如果你需要从高到低在比较器中改变
xy
到yx
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
}
}
}
You are never visiting the last element of the array. 你永远不会访问数组的最后一个元素。
Also, you should be aware that bubble sort is pretty inefficent and you could just use Arrays.sort()
. 此外,您应该知道冒泡排序非常无效,您可以使用
Arrays.sort()
。
You just need to write one string Arrays.sort(arr)
for low to high for Java 8. 您只需要为Java 8编写一个字符串
Arrays.sort(arr)
,从低到高 。
Arrays.sort(arr, Collections.reverseOrder())
for high to low Arrays.sort(arr, Collections.reverseOrder())
从高到低
In java8 you can do something like this: 在java8中,你可以这样做:
temp.stream()
.sorted((e1, e2) -> Integer.compare(e2, e1))
.forEach(e -> System.out.println(e));
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, it's very inefficient! 虽然,正如其他人所指出的那样,效率非常低! :-)
:-)
If you just want sort the int array: Use the quicksort... It's not a lot of code and it's N*lgN in avarage or N^2 in worst-case. 如果你只想对int数组进行排序:使用快速排序...这不是很多代码,在最坏情况下它是avarage中的N * lgN或N ^ 2。 To sort multiple data, use the Java Compare (as above) or a stable sorting algorithm
要对多个数据进行排序,请使用Java Compare(如上所述)或稳定的排序算法
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;
}
If you want to apply same logic as what you have done...by not using Arrays.sort...then following will help 如果你想要应用与你所做的相同的逻辑......不使用Arrays.sort ......那么下面的内容将有所帮助
int[] intArr = {5, 4, 3, 8, 9, 11, 3, 2, 9, 8, 7, 1, 22, 15, 67, 4, 17, 54};
//Low to high
for(int j=0; j<intArr.length-1; j++){
for(int i=0; i<intArr.length-1; i++){
if (intArr[i] > intArr[i+1]){
int temp = intArr[i+1];
intArr[i+1] = intArr[i];
intArr[i] = temp;
}
}
}
//High to low
for(int j=0; j<intArr.length-1; j++){
for(int i=0; i<intArr.length-1; i++){
if (intArr[i] < intArr[i+1]){
int temp = intArr[i+1];
intArr[i+1] = intArr[i];
intArr[i] = temp;
}
}
}
for(int ars : intArr){
System.out.print(ars+",");
}
You need a more efficient sort. 您需要更有效的排序。 like mergesort.
像mergesort。 try www.geekviewpoint.com and go to sort
尝试www.geekviewpoint.com并去排序
You can try with bubble sort: 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());
}
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]);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.