I’m trying to come up with an algorithm to sort
number array { 5,3,6,5,4} into the result {3,4,5,5,6} from lowest to highest highest.
I have a basic idea: treat element 0 to be your min variable. if that min is greater than the next element then swap. then test if second element is greater than the third element and swap. but the result are not coming out right
I came up with a swap formula but it only works for two elements
int temp = numArray[0];
numArray[0]= numArray[1];
numArray[1]= temp;
You’re looking for Bubblesort. Your method of just checking consecutive elements won’t work.
For example, it won’t work for the input
[1, 3, 2, 5, 4, 3, 6].