Hello
Another assignment I need help with. I need a program that will sort an array & then print out the original array & the sorted array side-by-side in 2 columns. I have figured out the sort part of the program but am having problems getting the original to print. The sort changes my original array entirely. I've tried a few different things but get really wild answers. I know I need to do 2 for loops to get them to print side-by-side.
I am able to use any type of sort. I chose the insertion sort.
I thought if in the beginning (before the sorting) if I could have the original array = to a new array to be called upon when the sorting is done, I could list the new array & the sort array. This hasn't worked for me.
ANY POINTERS IN THE RIGHT DIRECTION would be appreciated!! I have 5 programs left to write & I'm done with the class!
Thanks!!
Christine
#include <stdio.h>
#define NUMEL 10
void main(void)
{
int nums[NUMEL] = {22,5,67,98,45,32,101,99,73,10};
int i, moves;
int selection_sort(int [], int);
moves = selection_sort(nums, NUMEL);
printf("The sorted list, in ascending order, is:\n"
;
for (i=0; i < NUMEL; ++i)
printf(" %d \n ", nums);
}
int selection_sort(int num[], int numel)
{
int i, j, min, minidx, temp, moves = 0;
for ( i = 0; i < (numel - 1); i++)
{
min = num;
minidx = i;
for(j=i+1; j<numel; j++)
{
if (num[j] < min)
{
min = num[j];
minidx = j;
}
}
if (min < num)
{
temp = num;
num = min;
num[minidx] = temp;
moves++;
}
}
return(moves);
}
I am able to use any type of sort. I chose the insertion sort.
I thought if in the beginning (before the sorting) if I could have the original array = to a new array to be called upon when the sorting is done, I could list the new array & the sort array. This hasn't worked for me.
ANY POINTERS IN THE RIGHT DIRECTION would be appreciated!! I have 5 programs left to write & I'm done with the class!
Thanks!!
Christine
#include <stdio.h>
#define NUMEL 10
void main(void)
{
int nums[NUMEL] = {22,5,67,98,45,32,101,99,73,10};
int i, moves;
int selection_sort(int [], int);
moves = selection_sort(nums, NUMEL);
printf("The sorted list, in ascending order, is:\n"
for (i=0; i < NUMEL; ++i)
printf(" %d \n ", nums);
}
int selection_sort(int num[], int numel)
{
int i, j, min, minidx, temp, moves = 0;
for ( i = 0; i < (numel - 1); i++)
{
min = num;
minidx = i;
for(j=i+1; j<numel; j++)
{
if (num[j] < min)
{
min = num[j];
minidx = j;
}
}
if (min < num)
{
temp = num;
num = min;
num[minidx] = temp;
moves++;
}
}
return(moves);
}