Transcript for:
Sorting Arrays in C Programming

Hello everyone this is the program to sort an array in an ascending order. I am Milan Humirev from WeAreEngineer. Let's start this video. After watching this presentation you will learn to sort an array in an ascending order and to understand this example you should have some of the knowledge about C programming chapters that are array in C programming, C if else if statement and C for loop statement.

so all the links are given down in the description so please don't forget to check out first of all let's discuss about the problem this program will implement in one dimensional array and we will sort them in an ascending order so if we take a array with a random numbers like 45 56 in a random order and we will sort them in an ascending order like this 12 22 34 45 in ascending order so how we will do that right so step to solve the problem first of all we will create an array of fixed size that is maximum capacity let's say 7 right so here i made an array of 7 and i will take an n variable which store the number of elements of an array so let us suppose n equals to 7 there and we will iterate the loop and take an array element and print them so to sort we will use a nested loop and each loop will compare with all the elements below it what i'm trying to say is first of all we will check this value with all the numbers behind it and again with the second with all the numbers behind it and likewise In case the element is greater than the elements present below it then they were interchanged So the logic is if this is greater Number one is greater than number two, then it will exchange. Okay, so this two will be exchanged so after executing the nested loop we will obtain an array in an ascending order so let's do the program here we will take n variable which stores the number of elements of the array so let's write this program here i have have written the printf into the value of n and scanf person d and n so i took a value of n that is how many arrays elements are there in the array so after that what i did is i made an array of n variable which will ask an user for the number so I will iterate the loop to take array elements as an input so for taking the input I have done enter the numbers for i is equals to 0 i is less than n and i plus plus that means if the user enter 7 then the value will be n value will be 7 and iterate to the 7 loops and ask the i as an integer and put in the array so the array will be obtained as 45 56 like this as the number entered by the user so now we get the array now have to sort this array in the ascending order. So initially array elements will be localized by the 0, 1, 2, 3, 4, it will start with 0. So now to sort this array I have written this program. Here I will make you clear about how the ascending order had been defined. In first here for i is equals to 0 the zero value is 45 and it will go after that for j loop so i plus 1 that means j means i plus 1 that is 0 plus 1 and it will go with the 1 value of j so that is locating at 56. and if the number i is greater than number j that means if 45 is greater than 56 then what happened then the value became the false the logic became the false So what happened?

They will remain the same. So no exchange in this certain numbers. So 45 is lesser than 56. That means 45 should be in front of 56. That is true. Right. So after that.

again it will go after that it will terminate and again go to the first value it will be again 0 and I plus 1 now 1 plus 1 that will be 2 right so it will go to the next step to locate 2L right and after that yes 45 is smaller greater than 12 yeah it's become true right so after that what happened here the number will get exchanged a is equals to number i that means we are taking a as a temporary variable here and temporary variable will store number i that means 45 right so a is equals to 45 and here number i is equals to number j so number i means it's 45 holding this one right this is the space where the 45 was hold now that will be replaced by number j that means 2L. Now 45 will come at behind and 2L will go at 45 right. So now this value this space will get 2L.

So number j is equals to a that means here it will store the 45 value. So these values get interchanged. right so our next array become 12 56 and 45 right so like this third iteration will also implement so the same code i have copied here and here we get this array now it will again locate I is equals to 0 that is too well and it will go with 34 now because the J value is continuously increasing here and it will go up to the n value right if the n is around 7 then it will continue looping in 7 iteration okay so here in the 34 it will check again it's false and again the same array had been formed and likewise in the next loop what happens after the j completion it will be i will be 1 again 56 will be located and after that it will check all the numbers behind it as same before now it will locate to 45 and if that is true they will be exchanged right and the next loop and the next array become 12 45 56 so like this each every elements will check its back element and exchange if that is greater so this is the complete logic of sorting an array so after that 45 will go to 34 and if that is true then finally we get the ascending order array elements like this.

and we will print them simple error printing method printf arranged and i is equals to zero i is lesser n and like this number i we will print number i and we will get the answer printed so let's program and see the output all the other things are same as usual we did in array so i have declared an array and i have asked the number of elements from the user and I have asked the element as well so after that I have made a loop two loops one for the elements and for the back elements then I have swapped the value and simply print the ascending order as usual so let's execute the program here so here you can see it is asking me the number of elements so I will enter the number of elements So I will enter 7. So after that it is asking me the elements. So I will enter some of the elements. So here we go.

Let's see either it will be sorted or not. So you can see 2 is the smallest number here. 34, 34, 56, 67, 97 and 98. So we get the answer.

So if you like this presentation, please don't forget to subscribe, like and share. Thank you.