Sorting from smallest to largest in C

  • Thread starter Thread starter mossfan563
  • Start date Start date
  • Tags Tags
    Sorting
Click For Summary
SUMMARY

The discussion focuses on a C program designed to sort an array of structs containing random integers from smallest to largest. The code provided incorrectly sorts the array in descending order due to a flawed comparison in the sorting algorithm. Additionally, the program only swaps the 'a' values of the structs, neglecting the 'b' values, which may lead to data inconsistency. Key issues include an incorrect loop limit that risks accessing out-of-bounds memory and a confusing method structure that combines array initialization with printing.

PREREQUISITES
  • Understanding of C programming language syntax and structures
  • Knowledge of sorting algorithms, specifically bubble sort
  • Familiarity with memory management and array indexing in C
  • Experience with debugging techniques in C
NEXT STEPS
  • Refactor the sorting algorithm to implement a bubble sort that correctly sorts in ascending order
  • Learn about struct manipulation in C, focusing on swapping multiple fields
  • Explore memory safety practices to prevent out-of-bounds access in arrays
  • Investigate best practices for function design in C, separating initialization and output functions
USEFUL FOR

Students learning C programming, software developers working with data structures, and anyone interested in improving their understanding of sorting algorithms and memory management in C.

mossfan563
Messages
54
Reaction score
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.
 
Physics news on Phys.org
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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
9
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 1 ·
Replies
1
Views
6K