Sitemap
Hirushan Sajindra

Hirushan Sajindra

➤ PhD Researcher | Atlantic Technological University (ATU)

Quick sort Algorithm

5 min readApr 22, 2020

Here Quick sort algorithm is used for array sorted. Initially here, there are ten numbers are taken as user input to the array which has size 10 and known as “A”

User Input values

Here ten numbers are entered to the system by the user. Then original order of the entered numbers, ascending order of the entered numbers and median of the created ascending order are received as the output.

Press enter or click to view image in full size
Output

Source code is executed as below…

Normally the execution of source code is started from the main function. At first an integer type array called as “ A ” is created in the “ main function ”. It means that the data type of this array is integer type. It is an empty array which has size 10. After that a variable is created which is called as “ i ”(data type is integer) for running for loops. Median variable is also crated under the float data type. Here float data type is used because, decimal numbers are most tended to receive as values. Taking of user input (numbers) and storing of these numbers to the array “A” are done by the first for loop.

After that “ !!!!!!! original Array !!!!!!! “ is printed by the printf. After running of it, the user input numbers are printed according to the original order due to the running of next for loop. Here all the numbers which were input are printed. After that “ @@@@@ Sorted Array @@@@@ “ is printed by the printf. After printing that, a calling is occurred from this “qsort(A,0,9)” to the “ qsort “ function with three parameters. The parameters are the array called “ A ”, first index (0) of it and last index (9).In the qsort function a calling is occurred by the q = partition(A,p,r) to the function which is called “ partition ” for preparing the values which are input by the user according to the ascending order. qsort function is recursion function.

What is the recursion function ..

A Recursive function is a function that calls itself during its execution. When calling, the parameters takes less values than the parameters which are at the beginning of the function.

Get Hirushan Sajindra’s stories in your inbox

Join Medium for free to get updates from this writer.

More datails….

qsort function

An integer called “ q “ has been created in the qsort function. After that “p” value (first index of the “A” array) is checked that whether it is less or not than “ r ” value (last index of the “ A ”array) by an if condition. If the condition is true, the line, q = partition(A,p,r) is executed. In qsort function, a calling occurred to partition function with three parameters by the executing of this line. When the running of this line for the first time, the parameter values are which were the values sent from the main function to the qsort function. They are the array called “ A ”, first index in the array and the last index of the array. Partition function is as below.

Press enter or click to view image in full size
partition function

Partition function and remained two lines of the qsort function can be described using a diagram with the simple example as below. (This is not pseudocode and the array of the pseudocode is stared from 1 to n. Here the array is stared from 0 to n.)

Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size

Here, call is occurred with the shown three parameters from the qsort function to qsort function (recursion function). qsort function runs from start again. A calling is occurred to the partition function, if the if condition is true. Here length of the array from 0 to 5 is sorted.

Press enter or click to view image in full size

Here also, call is occurred with the shown three parameters from the qsort function to qsort function (recursion function). Here, calling is occurred to partition function again. Here length of the array from 7 to 9 is sorted.

As this way execution is occurred until if condition is false in the qsort function. After the sorting of array called “ A ” , this array is printed as one by one a for loop in the main function. As finally, to calculate median of sorted array, the values of 4th index (A[4]) and the 5th index (A[5]) are added together and the sum is divided by 2 and this value is printed.

--

--

Hirushan Sajindra
Hirushan Sajindra

Written by Hirushan Sajindra

➤ PhD Researcher | Atlantic Technological University (ATU)