Sorting from smallest to largest in C

  • Thread starter mossfan563
  • Start date
  • #1
54
0

Homework Statement


I have to create a program that will sort an array of structs with random numbers from smallest to largest.


Homework Equations


This is my code right now.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct
{
int a;
int b;
} pie;

void print_array(pie array[],int size);
void largest_a(pie array[],int size);

int main()
{
pie array[20];
int size = sizeof(array)/sizeof(array[0]);
printf("size of array is %d\n",size);
srand ( time(NULL) );

print_array(array,size);
largest_a(array,size);

return 0;
}

void print_array(pie array[],int size)
{
int i;
for(i = 0; i < size; i++)
{
array.a = rand() % 100 + 1;
array.b = rand() % 100 + 1;
}

for(i = 0;i < size; i++)
{
printf("array[%d].a = %d\n",i,array.a);
printf("array[%d].b = %d\n",i,array.b);
}
}

void largest_a(pie array[],int size)
{
int min = 0;
int i;
int j;
for(i = size-1; i>=0;i--)
{
for(j = 0;j <= i;j++)
{
if(array[j].a < array[j+1].a)
{
min = array[j].a;
array[j].a = array[j+1].a;
array[j+1].a = min;
}
}
printf("Array value %d\n",min);
}
}

The Attempt at a Solution


The result I get is the following.

size of array is 20
array[0].a = 19
array[0].b = 77
array[1].a = 16
array[1].b = 85
array[2].a = 25
array[2].b = 96
array[3].a = 8
array[3].b = 78
array[4].a = 2
array[4].b = 93
array[5].a = 50
array[5].b = 98
array[6].a = 59
array[6].b = 25
array[7].a = 53
array[7].b = 9
array[8].a = 31
array[8].b = 67
array[9].a = 89
array[9].b = 81
array[10].a = 100
array[10].b = 74
array[11].a = 90
array[11].b = 92
array[12].a = 18
array[12].b = 16
array[13].a = 29
array[13].b = 81
array[14].a = 20
array[14].b = 99
array[15].a = 76
array[15].b = 90
array[16].a = 28
array[16].b = 43
array[17].a = 26
array[17].b = 52
array[18].a = 38
array[18].b = 33
array[19].a = 29
array[19].b = 91
Array value 2
Array value 8
Array value 16
Array value 18
Array value 19
Array value 20
Array value 25
Array value 26
Array value 28
Array value 29
Array value 31
Array value 59
Array value 59
Array value 59
Array value 59
Array value 59
Array value 59
Array value 59
Array value 59
Array value 59

I'm trying to figure out why it stops sorting after a certain point.
Any ideas?
Thanks.
 

Answers and Replies

  • #2
Filip Larsen
Gold Member
1,420
332
Without running your code I can offer the following comments:

The limit for j in the inner loop is not correct and will make the code index the 21th element of your array, possibly overwriting memory it shouldn't.

You mention the code should sort in ascending order, but it really sorts the array in descending order (smallest a values end up in highest index).

When you swap elements, you only swap a. I don't know if this is intentional, but I would probably expect the b value to be swapped as well.

You print out only the smallest element that has been swapped for each run of the inner loop. If nothing has been swapped (because the elements already were in correct order) you print the min value from the previous run. This is likely why you see only 59 after a while - the remaining of the array from that point on is already in sorted order. Hint: after a run of the inner loop, you know the element in array must contain the smallest (or highest, if you change the ordering to ascending) so you can just print that. Or better yet, you can print out the whole array after the outer loop has finished (in which case you will also note that the ordering is reverse of what you like).

Having a method called print_array that also initializes the array with data is confusing. I suggest you make a different method for the initialization and let print_array just do printing; this also mean you can use print_array to print out the array both before and after your sorting loop.
 

Related Threads on Sorting from smallest to largest in C

Replies
4
Views
881
  • Last Post
Replies
1
Views
5K
Replies
3
Views
6K
Replies
7
Views
2K
  • Last Post
Replies
6
Views
2K
  • Last Post
Replies
4
Views
2K
Replies
2
Views
887
Replies
6
Views
2K
Replies
0
Views
5K
Replies
11
Views
11K
Top