The goal is to have the user input the amount of numbers they want sorted, then the numbers themself. This part works fine.

The sorting idea is to take the largest value, move it to the last value in the array, and then repeat for the rest of the array.

The problem is the function to sort the numbers, for example, the input and output I get from running this program: (the print of the numbers back was just to test that it was recieveing them correctly)

Enter amount of numbers to be sorted: 4

Enter numbers to sort: 8 7 2 9

8729

In sorted order: -4261520 0 -4261576 -13354412

Not exactly sure why it's doing this and what's causing it.

Any pointers would be much appreciated.

Code:

#include <stdio.h>
void selection_sort(int a[], int low, int high);
int split(int a[], int low, int high);
int main(void)
{
int i, n;
printf("Enter amount of numbers to be sorted: ");
scanf("%d", &n);
int a[n], b[n];
printf("Enter numbers to sort: ");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
selection_sort(a, 0, n - 1);
printf("In sorted order: ");
for (i = 0; i < n; i++)
printf("%d ", b[i]);
printf("\n");
return 0;
}
void selection_sort(int a[], int low, int high)
{
int i, max, temp1;
if (low == high) return;
for(i = 0; i < high; i++){
if (a[i] > max) {
max = a[i];
temp1 = a[high - 1];
a[i] = temp1;
a[high - 1] = max;
}
}
selection_sort(a, 0, high - 1);
}