public class sorting {
public static void sort(int arr[]) {
int n = arr.length;
int count = 0;
int tempArr [] = arr.clone();
// One by one move boundary of unsorted subarray
for (int i = 0; i < n-1; i++){
// compare proceeding element
int val = i;
for (int j = 0; j < n; j++){
if (arr[val] > arr[j])
count++;
System.out.println(count);
}
tempArr[count] = arr[val];
count = 0;//reset counter
}
for (int i = 0; i < n; ++i)
System.out.print(tempArr[i] + " ");
}
public static void main(String args[]) {
int a[] = {50, 40, 30, 20, 10};
sort(a);
}
}
I assume that this would sort the arrays in another array, but there is a problem where index 0 won't change. This uses a method of comparing the current value to all the elements in the array. Then using count as the index by incrementing it if the current key is greater than the proceeding values.
The problem is that it increments at the last part.
0
1
2
3
4
0
0
1
2
3
0
0
0
1
2
0
0
0
0
1 //it incremented
50 20 30 40 50