Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sorting an array 1

Status
Not open for further replies.

cjones

Technical User
Apr 7, 2001
22
0
0
US
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(&quot;The sorted list, in ascending order, is:\n&quot;);
for (i=0; i < NUMEL; ++i)
printf(&quot; %d \n &quot;, 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);
}





 
Just copy the array to preserve it's contents. Do something like this:

Code:
int OrgNums[NUMEL];
/* More declarations here(you didn't think I'd it ALL for ya?) */
 
for (i=0; i < NUMEL; i++)
    { OrgNums[i] = nums[i]; }

/* Do your sorting */

for (i=0; i < NUMEL; i++)
    { printf(&quot; %d      %d\n &quot;, nums[i], OrgNums[i]); }


Kim_Christensen@telus.net
 
Yeh! that should work. Just another point for you, if you #define something its classed as global, so there is no need to pass it in the function call.
 
To clarify a bit: A #define isn't global in the same sense that a non-static file scope variable is global. #define is a preprocessor directive, so the preprocessor replaces all occurences of the token, in this case NUMEL, with the associated value.

But yes, there is no need to pass it in a function call.

Russ
bobbitts@hotmail.com
 
Just Create two identical arrays by copying the first array nto another thereafter sort either of them and use this statement :

for(i=0; i<EndLimit; i++)
{
printf(&quot;%d \t %d&quot;,arr,anotherArr);
}

SwapSawe.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top