C Programming Tutorial – 41 – Sorting Arrays

GitHub – https://github.com/buckyroberts
reddit – https://www.reddit.com/r/thenewboston/
Support – https://www.patreon.com/thenewboston
thenewboston – https://thenewboston.com/

• Views:173,283 views
• Rating:
•  Click on the stars above to rate this page
• Categories:
• Tags:

Tu He says:

i think (rand()%25)+1 generates random number between 1-25, instead of 0-25, Bucky. No worries, still love you.

Asif Ahmed says:

finally, I have understood it .thanks bucky bro!

jacob hiro says:

errors for line 11 [howMany] . “expression must have a constant value” “expected constant expression” ” cannot allocate an array of constant size()” ” ‘goals’ : unknown size” even I copied this all code correctly and pasted it, those errors pop up.. plz help me someone..

Bhavya Bhatia says:

in which part i can find 2d array ?

zuher abud says:

please bucky organise your code for us plz:
int i, temp, swapped;
int howMany = 10;
int goals[15]; //here plz use different no(s) so that we dont get confused.

printf(“Original Listn”); //in the other list you kept the printf b4 the loop plz be uniform
for(i=0; i

Faith Efm says:

You’ve made this the easiest thing to understand. Thank you!

What if I want to make a random number from 50 to 99? How can I put like a minimum number for the random numbers that will be printed?

Matthias Novak says:

here’s my attempt on bubble sort using chars

/*
Feel free to comment. TIA
*/
#include
#include

int main()
{
int i, swapped;
char name[5] = {‘B’,’F’,’C’,’A’,’X’};
char temp;

printf(“Unsorted List!n”);

for (i=0; i<5; i++)
{

printf("%cn", name[i]);

}

while (1)
{
swapped = 0;

for (i=0; i < 5; i++) { if (name[i] > name[i+1])
{
temp = name[i];
name[i] = name[i+1];
name[i+1] = temp;
swapped = 1;
}
}

if (swapped == 0)
{
break;
}

}
printf(“nnSorted List!n”);
for (i=0; i < 5; i++) { printf("%cn", name[i]); } return 0; }

Denis Otim says:

-_- I don’t understand whats going on with the “swapped” variable. NEED HELP!!!

shizyninjarocks says:

scimitar4211 says:

can you make a video on how to compare the numbers in an array from user input? A frequency program that will count how many of the number(s) are the same?

Kalim says:

Why does he specifiying int in the middle of the code, when he already did in the beginning?

mst cr says:

WHY DOESN’T IT WORK WHEN I PUT SWAPPED = 0 BEFORE WHILE LOOP ??

Swordsmaiden Music says:

I like to imagine the switching process here as a bit like copy and paste!

TeamPlaythrough says:

can someone help me and tell me why this code doesnt work. I would really appreciate that

#include
#include
#include
#include
#include

int main()
{
int counter, temp, swapped;
int howMany = 10;
int goals[howMany];

printf(“Original list: n”);
for(counter = 0; counter < howMany; counter++) { goals[counter] = (rand()%25) + 1; printf("%d n", goals[counter]); } while(1) { swapped = 0; for(counter = 0; counter < howMany-1; counter++) { if(goals[counter] > goals[counter+1])
{
int temp = goals[counter];
goals[counter] = goals[counter+1];
goals[counter+1] = temp;
swapped = 1;
}

}
if (swapped == 0)
break;
}
printf(“nsorted list: n”);
for(counter = 0; counter < howMany; counter++); { printf("%d n", goals[counter]); } return 0; }

Ren Voly says:

What is use of [i]? Or [i+1]?(sry for bad eng)

thanks 🙂

cy world says:

While starts endless loop
The variable swapped flags condition as sorted, defined as 0, (1 means unsorted)
The for loop embedded exchanges current with next value position if current>next
for 10 times.
Once out of the for loop, if the array isn’t sorted, swapped will have the last value of 1
meaning not sorted.
The following if statement will not break and will go back up to while and redefine
swapped as sorted, i.e. equal to 0 again.
Then tje for loop embedded will go through the sort again. If this time all values are sorted
then the value of swapped will remain 0 and the while loop will break once it hit the outer
if statement
Array is now sorted.
Keeping track of the brackets associated with while, for, and if statement is crucial to avoid confusion. Here my program:

int main()
{

int i, temp, swapped; //i counter, temp swapped
int howMany = 10; //how many variables
int goals[howMany]; //array containing numbers to sort

for (i=0; igoals[i+1]) //loop this howMany times
{
int temp = goals[i];
goals[i] = goals[i+1];
goals[i+1] = temp;
swapped = 1;
}

}

if (swapped == 0)
{
break; //once outside the for loop, break
//array is fully sorted by for loop
}

}

printf(“nSorted Listn”);
for(i=0; i

DerSohnDesAlvaters says:

I don’t know what is wrong, but i keep get the same number but it is not arranged in a column… It looks like this 4196333
I typed everything that Bucky did.

#include
#include
#include
#include
#include

int main()
{
int i, temp, swapped;
int howMany = 10;
int goals[howMany];

for(i=0; i

Mohammad Amir Darbed says:

The random number generator code generates numbers between 1 to 25, and not 0 to 24, because of +1 there. am I right?
thanks for the great videos, Bucky 🙂

Mohammad Arafat Intisher says:

Thanks, Man. Its very helpful.

Lim Kim Sreng says:

#include
#include
#include

main()
{
int ctr, inner, outer, didSwap, temp;
int nums[10];
time_t t;

//If you don’t include this statement, your program will always generate the same 10 random numbers
srand(time(&t));

//The first step is to fill the array with random numbers from (1-100)
for (ctr = 0; ctr < 10; ctr++) { nums[ctr] = (rand() % 99) + 1; } //Now list the array as it currently is before sorting puts("nHere is the list before the sort:"); for (ctr = 0; ctr < 10; ctr++) { printf("%dn", nums[ctr]); } //Sort the array for (outer = 0; outer < 9; outer++) { didSwap = 0; //Become 1 (true) if list is not yet ordered for (inner = outer; inner < 10; inner++) { if (nums[inner] < nums[outer]) { temp = nums[inner]; nums[inner] = nums[outer]; nums[outer] = temp; didSwap = 1; } } if (didSwap == 0) { break; } } //Now list the array as it currently is after sorting puts("nHere is the list after the sort:"); for (ctr = 0; ctr < 10; ctr++) { printf("%dn", nums[ctr]); } return (0); }

laith barghothi says:

way easier than I thought ! THANKS 🙂

For those who dont get temp variable, imagine you have a cup of juice and a cup of milk, and you want to switch the liquids in those cups so in the milk cup there is juice and vice versa. You cant just pour milk in the juice cup so you take third cup and do the following:
1)Pour milk in the third(empty) cup
2)Then pour juice from the juice to the milk cup
3)From that third cup(which now has milk) pour milk in the now empty juice cup
Hope this helps

Dawood Aijaz says:

Can anyone tell me what is the purpose of while loop.

bookitup says:

for bubble sort -program if i take input from user of what nummber of elements he want to sort what changes will occur

young kenny says:

For Those you may not understand. The order of setting value like “=” between “temp” and “array[i]”. They are From Right to left.
Not From Left to right that how we read. Imagine the right side code is “agent smith” from “Matrix”, and “Agent Smith” is trying to make left code “Mr.anderson” become one of him. int value = 2; means this “2” is “agent smith” ,and value is “Mr.anderson”, and 2 just made Value become another 2. so, when temp=array[0]; means array[0] set temp a value, no the otherway around, and in the end, value made the array[1] become array[0]. This should help you guys . And if you don’t ? Watch 1 2 3. It’s a movie about programmers.

manoj y says:

While(1){
swapped==0;
for(i=0;I<9;i++){
If(goal[i]>goal[i+1]){
temp=goal[i];
goal[i]=goal[i+1];
goal[i+1]=temp;
Swapped==1;
}

}
If(swapped==0){
break;
}

Dogba Djaze says:

I did the same looping for the bubble in a function, but it works for any array except an array of two elements. Can you help?

Hà Chí Hào says:

why you use int howMany = 0; instead of just write a number 10 ?

Moses Valentin says:

Very bad tutorial, no explanation and just writing code.

Anh Huynh says:

you initialized “temp” twice (once on the 1st line and one in the for loop). Is that correct? How can the code still be compiled?

AJay Gupta says:

For those of you who re not getting it, keep workimg on it, don’t give up! it took me almost a year to understand this piece of code. If I can do it than anyone can sort arrays!!

Gaming Turkey says:

First time I found a Bucky program difficult. I liked the challenge, keep at it man!

Iqbal Kisas says:

I don’t understand why howmany-1 works, I dont see the difference of this with howmany

Armaan McLeod says:

You can also do it this way:
#include
#include

#define MAXSIZE 10

void read_into_array(int A[], int n);
void print_arrays(int A[], int n);
void sort_arrays(int A[], int n);
void int_swaps(int *, int *);

int
main(void){
int A[MAXSIZE];

printf(“Original Listn”);
print_arrays(A, MAXSIZE);

sort_arrays(A, MAXSIZE);

printf(“Sorted Listn”);
print_arrays(A, MAXSIZE);

return 0;
}

void
read_into_array(int A[], int n){
int i;
for (i = 0; i < n; i++) { A[i] = (rand() % 25) + 1; } } void print_arrays(int A[], int n){ int i; for (i = 0; i < n; i++) { printf("%dn", A[i]); } printf("n"); } void sort_arrays(int A[], int n){ int didswaps, i; didswaps = 1; while (didswaps) { didswaps = 0; for (i = 0; i < n-1; i++) { if (A[i] > A[i+1]) {
int_swaps(&A[i], &A[i+1]);
didswaps = 1;
}
}
}
}

void
int_swaps(int *x1, int *x2){
int temp;
temp = *x1;
*x1 = *x2;
*x2 = temp;
}

Amin Fard says:

To sort thing from high to low: if (goals[i] < goals[i+1]) {

Zaim Rashdan says:

why my rand() function always generating the same sequence of random number everytime I run it? Why it’s not generating the new random number just like bucky did in this video? Can someone please explain it to me?

Yura Er says:

line 29. i think you don´t need to initialize “temp” each time in the loop, because you did it in the line 9. am i correct ?

classicgamelover1985 says:

Too tiny, I can’t follow along with your text… sawweee[sorry]

linh nguyen says:

wow, so complicated