C Programming Tutorial - 41 - Sorting Arrays

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

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

pavlevod
Автор

When I tried writing the process down on paper, I finally came to the realization of how fast computers are.

UsernameDP
Автор

A good way of swapping variables without declaring the third variable (e.g. "temp"):

goals[i] += goals[i + 1];
goals[i + 1] = goals[i] - goals[i + 1];
goals[i] -= goals[i + 1];

mk_freestyle
Автор

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!!

ajay
Автор

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 <Matrix> 1 2 3. It's a movie about programmers.

olddriver
Автор

My acidity attacked me when I tried figuring this thing on my own, tysm for this video.

regienaldcabanamontalvo
Автор

A code for testing that makes the output in screen a little easier to read and shows the user how many times loops were executed:


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

int main(void)
{
int swapped, tmp;
int arrsize = 100;
int arr[arrsize];
srand(time(NULL));
int counter = 0;

printf("Generated array: [");
for (int i = 0; i < arrsize; i++)
{
arr[i] = (rand() % 25) + 1;
if (i == arrsize - 1)
printf("%d]\n", arr[i]);
else
printf("%d, ", arr[i]);
counter++;
}

while (1)
{
swapped = 0;
for (int i = 1; i < arrsize; i++)
{
if (arr[i] < arr[i - 1])
{
tmp = arr[i];
arr[i] = arr[i -1];
arr[i - 1] = tmp;
swapped = 1;
}
counter++;
}
if (swapped == 0)
{
break;
}
}

printf("Sorted array: [");
for (int i = 0; i < arrsize; i++)
{
if (i == arrsize -1)
printf("%d]\n", arr[i]);
else
printf("%d, ", arr[i]);
counter++;
}

printf("\nIn total, there were %d loop executions!\n", counter);
return 0;
}

rafaelnavarrocanizarescorr
Автор

The code works in Java as well, I just always end up having to go ahead and define i= 0 to get my for loops going. Thank You!!

milesbaker
Автор

You beautiful bastard, you do what my lecturer's struggle to do, and that's actually make sense

JRockPR
Автор

To sort in decreasing order, just swap ">" with "<".
It's that easy!

ReddoX
Автор

I've changed my algorithm a bit in case anyone finds this usefull.
#include<stdio.h>
int main()

{

int a, b, c, i, j, temp, swapped;

printf("Enter size of the array\n");
scanf("%d", &a);

int array[a];

printf("Enter values\n");

for(i=0;i<a;i++)
{
scanf("%d", &array[i]);

}

printf("Original array is\n");

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

printf("%d\n", array[i]);
}

for(i=0;i<a-1;i++)
{
for(j=0;j<a-1-i;j++)
{
if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}


}



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

}

chiefmiester
Автор

Btw,
for swapping you can use :
int x1=10;
int x2 =12;

x2=x2+x1;
x1=x2-x1;
x2=x2-x1;

//now x1=12 & x2=10/

this work.

liorbm
Автор

Your tutorials are awesome... Short and informative in the simplest possible way.. Thanks man... Just a request, please make the font size bigger next time you make any tutorial series on programming.. :)

JD-hqkn
Автор

I have been trouble with arrays. Your videos are really helping me. Thanks.

bigmacdoubleyouv
Автор

Bro, I just want to say thank you so much for your help. Your videos have helped me a lot you are carrying my c class right now

mikerojas
Автор

my code prints out random bunch of combinations of integers but then i realized i put the symbol '&' in the printf part. anyway i cleared it up and it workeD!! thank u mr.codeguy

batuamparh
Автор

If you have a loop that is like "While(5 > x){}" then the loop is basically saying "While 5 > x is true, run this code." Similarly if we do "While(true){}" its like "While true is true, run this code". That would run forever unless it was told to stop with a break statement because always true = true. Computers think 1 = true and 0 = false. While(1){} is just a short hand way of saying While(true=true){}.
The while loop in this lesson is basically saying "Keep looping through all the numbers" And then the break says "until the swapped variable says we are done sorting".

Jemmeh
Автор

If you are struggling to understand this sorting algorithm, the selection sort algorithm is a pretty simple one to try and understand as well.

Fabhaz
Автор

You declared the temp variable again inside the while loop, so it was useless to declare it in the beginning of the main function. "int temp[i] = goals[i];" should be changed to "temp[i] = goals[i];"

idansx
Автор

I have a problem ... when i use the rand function it gives me a list of random numbers but always the same list... what shoul i do ?

Iliealex