Hi, this is sorting selection program that i'm writing. The sorting works fine, however, I don't know how to make it so that the program sort a group of values then go to the next group of values to sort again.
Such as, if user's input: "32 1 92 23 -1 43 19 22 -2". The program will sort the first group (32 1 92 23) but I don't know how to make it so that it will sort the second group (43 19 22). The "-1" indicates next group, the "-2" indicates the end of the array.
Code:
#include <stdio.h>
#define NUM_INT 1000
void selection_sort(int x[], int size);
int get_maxpos(int x[], int size);
void print_array(int x[], int size);
int main()
{
int input[NUM_INT] = {0};
int i;
int j = 0;
printf("please input arrays of ints to be sorted ");
for (i = 0; i < NUM_INT; i++)
{
scanf("%d", &input[i]);
if(input[i] == -1) //NOT SURE WHAT TO DO HERE ?????????????????? TROUBLE SPOT!!
{
i = 0;
printf("%d\n ", input[i]); //TESTING
}
if(input[i] == -2) break;
if(input[i] < -2)
{
printf("One of the integers is less than -2");
}
/*
printf("Original array:\n");
print_array(input, i);
selection_sort(input, i);
printf("Sorted array:\n");
print_array(input, i);
*/
// printf("FIRST READ IS: %d\n ", input[i]); //TESTING
}
printf("Original array:\n");
print_array(input, i);
selection_sort(input, i);
printf("\nSorted array:\n");
print_array(input, i);
//IS THERE A WAY TO GO BACK TO THE FOR LOOP TO READ THE SECOND GROUP AT THIS POINT???
return 0;
}
void selection_sort(int x[], int size)
{
int eff_size, maxpos, tmp;
for (eff_size = size; eff_size > 1; eff_size--)
{
maxpos = get_maxpos(x, eff_size);
tmp = x[maxpos];
x[maxpos] = x[eff_size -1];
x[eff_size -1] = tmp;
}
}
int get_maxpos(int x[], int size)
{
int i, maxpos = 0;
for (i = 0; i < size; i++)
maxpos = x[i] > x[maxpos] ? i: maxpos;
return maxpos;
}
void print_array(int x[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", x[i]);
}