I wrote some sorting code:
// A function to print an int array
void printArray(int arr[], int size) {
// Loop through the array elements
for (int i = 0; i < size; i++) {
// Print the current element with a space
cout << arr[i] << " ";
}
// Print a new line at the end
cout << endl;
}
void mikesort(int a[], int n)
{
if (n == 0 || n == 1) return;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - 1; j++)
if (a[j] > a[j + 1]) swap(a[j], a[j + 1]);
}
void testsort(void)
{
int a[] = { 5,4,8,2,1 };
printArray(a, 5);
mikesort(a, 5);
printArray(a, 5);
}
It is like bubble sort but easier to remember. It looks like it works.
Anyone know how to prove if it works or not? Does this algorithm have a name?
Thanks.