BubbleSorting C language - c

We are learning arrays and I just came around bubble sorting.
I wrote the following code to sort the array in ascending order but there is a problem.
I can't find it out but I know there's a problem.
I have found the correct code but I still don't understand why this is not working.
My Code
int a;
int i;
int temp;
int value[5] = { 5, 4, 3, 2, 1 };
for (i = 0; i < 5; i++) {
if (value[i] > value[i + 1]) {
temp = value[i];
value[i] = value[i + 1];
value[i + 1] = temp;
}
}
for (a = 0; a < 5; a++) {
printf(" %d ", value[i]);
}

Your Code Has Multiple Problems ,
First And Foremost You are printing the array elements after that sorting that you did with value[i] and you are running loop with variable a.
for(a=0;a<5;a++){ //You Are Incrementing a
printf(" %d ",value[i]); //But Using i here , change it to a.
//As It will just print the value[i] member
}
You are Accessing value[5] , which is not yours.
for(i=0;i<5;i++){ //Here In The Last Loop value[4] is compared with value[5],
// value[5] is not defined
//for(i=0;i<4;i++)
//Instead Run Loop Till i<4 , I guess this is what you
//wanted but accidently made a mistake.
if(value[i]>value[i+1])
The Biggest Problem, is that you have not understood Bubblesort completely.
In Bubble Sort The Loop is run multiple times, until in a loop there are no member to swap, that is when you stop looping.
This is What Wikipedia Says
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller or larger elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort.It can be practical if the input is usually in sorted order but may occasionally have some out-of-order elements nearly in position.
See The Below Presentation To Understand How Bubble Sort Works.See the Loop Run again and again, till there are no member left to swap.
Step-by-step example
Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort. In each step, elements written in bold are being compared. Three passes will be required.
First Pass
( 5 1 4 2 8 ) to ( 1 5 4 2 8 ), Here algorithm compares the first 2 elements,and swap since 5 > 1.
( 1 5 4 2 8 ) to ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) to ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) to ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.
Second Pass
( 1 4 2 5 8 ) to ( 1 4 2 5 8 )
( 1 4 2 5 8 ) to ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) to ( 1 2 4 5 8 )
Now, the array is already sorted, but the algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
Third Pass
( 1 2 4 5 8 ) to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) to ( 1 2 4 5 8 )
So You Need To Run The Loop Multiple Times Until No Member Are There To Swap, You Can Do So By Having a new variable count, initially initialised to 1 at the start, and before each loop starts, it get initialised to 0, if in the loop, Swap statement executes then, it changes to 1, and again the loop is executed because count is 1, if in the last pass, its value does not changes to 1 because all member are now sorted, so loop does not run again.

When i == 4 you access the position 5 with value[i+1] and it is not yours.
You are accessing unreserved memory.

Related

Merge Process In-Place:

Merge Procedure in MergeSort cannot run in-place.
This is my explanation:
Does it add up?
A = 5,1,9,2,10,0
”q”: pointer to the middle of array at index 2.
We are merging element at q and those to its left with the elements to the right of q.
A= 1,5,10,0,2,12
We point to the beginning of the left side with ”i” and to that of the right with ”j”.
We point to the current position in the array with ”k”.
The algorithm starts with i=0, j=3, k=0.
If we merge in place:for k=0:i= 0, j= 3,0<1 -> A[k] =A[j] andj+ +
resulting array:A={0,5,10,0,2,12}
As we can see, we lost the value 1 already.
We will continue to lose values, for example in the next iteration:
for k=1:i= 0, j= 4,0<2 =⇒A[k] =A[i] andi+ +
resulting array:A={0,0,10,0,2,12}
It can be done using rotates of sub-arrays. There are in place merge sorts with O(n log(n)) time complexity, but these use a portion of the array as working storage. If stability is needed, then some small subset of unique values (like 2 sqrt(n)) are used to provide the space, since reordering unique values before sorting won't break stability. Getting back to to simple rotate algorithm:
1 5 10 0 2 12
0 1 5 10 2 12 0 < 1, rotate 0 into place, adjust working indexes
0 1 5 10 2 12 1 < 2, continue
0 1 2 5 10 12 2 < 5, rotate 2 into place, adjust working indexes
0 1 2 5 10 12 5 < 12, continue
0 1 2 5 10 12 10 < 12, continue, end of left run, done

Find the last smaller or equal number for every element in the array?

I have been given an array. I need to find the last (right-most) smaller or equal number for every element in the array.
For example:
2 5 1 6 4 7
2 has last smaller or equal number 1,
5 has last smaller or equal number 4 and not 1, etc.
Another example:
5 100 8 7 6 5 4 3 2 1
Here, every element has last smaller or equal number 1. I know the naive approach, i.e. O(n²), but need a better approach.
You could go from right to left and build a min array of the minimum number until now.
For your example 2 5 1 6 4 7, it would be:
Start at rightmost position:
7
4 7 (4 < 7)
4 4 7 (6 > 4)
...
So the min array for your example would be: 1 1 1 4 4 7
Now for each query, you start at the same position in the min array and go right until finding a number which is greater:
For 2:
2 5 1 6 4 7
1 1 1 4 4 7
^
------^
First element greater than 2 is 4, so return number just before = 1
For 5:
2 5 1 6 4 7
1 1 1 4 4 7
^
----------^
First element greater than 5 is 7, so return number just before = 4
To find efficiently the first element greater for each element in the input array you can use upper_bound algorith (example in C++ http://www.cplusplus.com/reference/algorithm/upper_bound/) to find the first element which is greater
Upper_bound takes log(N) time, so overall time to process every element in input array is O(NlogN)
Memory complexity is linear for the min array

How can I get no. of Swap operations to form the 2nd Array

I have got two arrays with same elements... (But in different order)
e.g 1 2 12 9 7 15 22 30
and 1 2 7 12 9 20 15 22
how many swaps operations are needed to form the 2nd array from the first.?
I have tried taking no. of different elements for each index and dividing the result by 2 but that isn't fetching me the right answer...
One classic algorithm seems to be permutation cycles (https://en.m.wikipedia.org/wiki/Cycle_notation#Cycle_notation). The number of swaps needed equals the total number of elements subtracted by the number of cycles.
For example:
1 2 3 4 5
2 5 4 3 1
Start with 1 and follow the cycle:
1 down to 2, 2 down to 5, 5 down to 1.
1 -> 2 -> 5 -> 1
3 -> 4 -> 3
We would need to swap index 1 with 5, then index 5 with 2; as well as index 3 with index 4. Altogether 3 swaps or n - 2. We subtract n by the number of cycles since cycle elements together total n and each cycle represents a swap less than the number of elements in it.
1) re-index elements from 0 to n-1. In your example, arrayA becomes 0..7 and arrayB becomes 0 1 4 2 3 7 5 6.
2) sort the second array using your swapping algorithm and count the number of operations.
A bit naive, but I think you can use recursion as follows (pseudo code):
function count_swaps(arr1, arr2):
unless both arrays contain the same objects return false
if arr1.len <= 1 return 0
else
if arr1[0] == arr2[0] return count_swaps(arr1.tail, arr2.tail)
else
arr2_tail = arr2.tail
i = index_of arr1[0] in arr2_tail
arr2_tail[i] = arr2[0]
return 1+count_swaps(arr1.tail, arr2_tail)
Here's a ruby implementation:
require 'set'
def count_swaps(a1, a2)
raise "Arrays do not have the same objects: #{a1} #{a2}" unless a1.length == a2.length && Set[*a1]==Set[*a2]
return count_swap_rec(a1, a2)
end
def count_swap_rec(a1, a2)
return 0 if a1.length <= 1
return count_swaps(a1[1..-1], a2[1..-1]) if a1[0] == a2[0]
a2_tail = a2[1..-1]
a2_tail[a2_tail.find_index(a1[0])] = a2[0]
return 1 + count_swaps(a1[1..-1], a2_tail)
end

Sorting an array where the only permitted operation is swapping 0 and another element

Given any permutation of the numbers {0, 1, 2,..., N-1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may apply the swap operations in the following way:
Swap(0, 1) => {4, 1, 2, 0, 3}
Swap(0, 3) => {4, 1, 2, 3, 0}
Swap(0, 4) => {0, 1, 2, 3, 4}
Now you are asked to find the minimum number of swaps need to sort the given permutation of the first N nonnegative integers.
This question comes from this website. I've included my code below, which takes too long to solve the problem. How can I solve this efficiently?
#include "stdio.h"
#define MAX 100001
void swap(int i);
int a[MAX];
int m,count=0,zeroindex,next=1;
int check()
{
int i;
if(a[0]!=0)
return -1;
else
{
for(i=next;i<m;i++)
{
if(a[i]!=i)
{
swap(i);
next=i; //just start form the next
return -1;
}
}
return 1;
}
}
main()
{
int n,i=0,j;
scanf("%d",&m);
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
if(a[i]==0)
zeroindex=i;
}
while(check()!=1)
{
for(i=0;i<m;i++)
{
if(a[i]==zeroindex)
{
swap(i);
break;
}
}
}
printf("%d",count);
}
void swap(int i)
{
int temp=a[zeroindex];
a[zeroindex]=a[i];
a[i]=temp;
zeroindex=i;
count++;
}
I think that the optimal solution to this problem involves finding a cycle decomposition of the original array and using that to guide what swaps you make.
There's a convenient notation for representing permutations in terms of smaller cycles within the permutation. For example, consider the array
5 4 0 1 3 2 7 6
The cycle decomposition of this array is (0 2 5)(1 3 4)(6 7). To see where this comes from, start by looking at the number 0. 0 is currently in the position that 2 should occupy. The number 2 is in the spot that 5 should occupy, and 5 is in the position that 0 should occupy. In this sense, the permutation contains a "cycle" (0 2 5) indicating that 0 is in 2's place, 2 is in 5's place, and (wrapping around) 5 is in 0's place. Independently, look at the number 1. 1 is in the spot where 3 should be, 3 is in the spot where 4 should be, and 4 is in the spot where 1 should be. Therefore, there's another cycle (1 3 4) in this permutation. The last cycle is (6 7).
In general, if you're allowed to make arbitrary swaps, the series of swaps required to sort a permutation that uses the fewest number of swaps works by splitting the permutation apart into its cycle decomposition and then using the swaps to repair the permutation. For example, to fix the cycle (0 2 5), the fastest option would be to swap 0 with 5, then swap 5 with 2. (This gives rise to the cycle sort algorithm).
We can adapt this idea here. Going back to our initial array 5 4 0 1 3 2 7 6, which has cycle decomposition (0 2 5)(1 3 4)(6 7), suppose that we want to fix the cycle (1 3 4). To do so, we can do the following:
5 4 0 1 3 2 7 6
5 4 1 0 3 2 7 6 (Swap 0 and 1)
5 4 1 3 0 2 7 6 (Swap 0 and 3)
5 0 1 3 4 2 7 6 (Swap 0 and 4)
5 1 0 3 4 2 7 6 (Swap 0 and 1)
Notice that the elements 1, 3, and 4 are now in the right place. The cycle decomposition of what remains is now (0 2 5)(6 7).
We can fix (6 7) as follows:
5 1 0 3 4 2 7 6
5 1 6 3 4 2 7 0 (Swap 0 and 6)
5 1 6 3 4 2 0 7 (Swap 0 and 7)
5 1 0 3 4 2 6 7 (Swap 0 and 6)
Now 6 and 7 are in the right place, and our remaining cycle decomposition is (0 5 2). That can easily be fixed:
5 1 0 3 4 2 6 7
5 1 2 3 4 0 6 7 (Swap 0 and 2)
0 1 2 3 4 5 6 7 (Swap 0 and 5)
More generally, this works as follows. To fix a cycle (c1, c2, ..., cn) that does not contain 0, do the following swaps:
Swap(0, c1)
Swap(0, c2)
Swap(0, c3)
...
Swap(0, cn)
Swap(0, c1)
To fix a cycle (0, c1, c2, ..., cn), do the following:
Swap(0, cn)
...
Swap(0, c3)
Swap(0, c2)
Swap(0, c1)
The challenge remains to show that this solution is optimal. To do so, let's begin by thinking about the number of swaps made. Suppose that we have a cycle decomposition for the array that contains cycles σ1, σ2, ..., σn. Fixing any individual cycle that does not contain zero requires k + 1 swaps, where k is the number of elements in the cycle. Fixing a cycle that does contain 0 requires k - 1 swaps, where k is the number of elements in the cycle. If we sum this up over all the cycles, we get that the cost is given by the total number of elements that are out of place (call that X), plus the number of cycles (call that C), minus two if 0 happens to be in a cycle (let Z be -2 if zero is out of place and 0 otherwise). In other words, the cost is X + C + Z.
I'm going to claim that there is no possible way to do better than this. Focus on any one individual cycle in the cycle decomposition (and, for simplicity, assume that the cycle doesn't contain zero). To move all of the elements in this cycle back to their original positions, every element in the cycle needs to be moved at least once. Since each swap involves the number 0, the number of total swaps necessary to move every item in the cycle at least once is k, where k is the total number of elements in the cycle. Additionally, we need one more swap because the very first swap we do introduces 0 into the cycle and we need to do another swap to get it out. A similar line of reasoning accounts for why a cycle containing 0 must use at least k - 1 swaps. Overall, this means that the optimal series of swaps is formed by getting the cycle decomposition and using the above approach to swap everything around.
The last question is how to find the cycle decomposition. This, fortunately, can be done efficiently. Let's go back to our original sample array:
5 4 0 1 3 2 7 6
Let's create an auxiliary array mapping each element to its index, which we can fill in in a single pass:
2 3 5 4 1 0 7 6
We can now construct the cycle decomposition as follows. Let's start at 0. The auxiliary array says that 0 is in position 2. Looking at position 2, we see that 2 is in position 5. Looking at position 5, we see that 5 is in index 0. This gives us the cycle (0 2 5). We can repeat this process across the array to build up the cycle decomposition, and from then can just play out the appropriate swaps to sort everything optimally.
Overall, the total time required is O(n), the total space required is O(n), and the number of swaps performed is minimized.
Hope this helps!
A simple approach:
1. Find all cycles of elements through their original positions.
2. Store size of each cycle in an array L[].
3. Initialise ans = 0.
4. While i = 1 to L.size():
if A[0] is not included in current cycle:
ans = ans + L[i] + 1;
else
ans = ans + L[i];
swap elements in array equivalently to correct this cycle.

K&R Qsort example with Pointers and Arrays confusion

I find it difficult to understand the following snippet of code. I understand the pointer to function mannerism showed, but where I find confusion is in the indicated lines.
void qsort(void **v, int left, int right, int (*comp) (void *, void *))
{
int i, last;
void swap(int **v, int i, int j);
if (left >= right) /* do nothing if array contains */
return; /* fewer than two elements */
swap(v, left, (left + right)/2); /* move partition elem */ [1]
last = left; /* to v[0] */ [2]
for (i = left + 1; i <= right; i++) /* partition */ [3]
if ((*comp) (v[i], v[left]) < 0) [4]
swap(v, ++last, i); [5]
swap(v, left, last); /* restore partition elem */ [6]
qsort(v, left, last - 1); [7]
qsort(v, last + 1, right); [8]
}
Can someone explain this routine to me, especially the indicated lines, just tell me what it's doing, because I can't figure this qsort out, the eskimo guide I read whilst reading k&r said that the qsort routine is rubbish, and overly complicated. I just need to understand why it's written like this, because it makes no sense to me.
Thanks, if for nothing, for reading this.
This is a beautiful piece of code!
First off, it's important that you understand the idea behind quicksort:
1) Take a list of numbers.
2) Pick one, call it X.
3) Make two lists, one of all the numbers smaller than X, and one of all the numbers bigger.
4) Sort the numbers smaller than X. Sort the numbers bigger than X.
The idea is that if we get lucky and pick a good value for X, then the list of numbers smaller than X is the same size as the list of numbers bigger than X. If we start with 2*N+1 numbers, then we get two lists of N numbers. Each time, we hope to divide by two, but we have to sort N numbers. How many times can we divide N by two? That's Log(N).
So, we sort N Log(N) times. This is great!
As for how the code works, here's the runthrough, with a little sketch. We'll pick a small array :)
here's our array: [DACBE]
at the start, left=0, pointing to D. right=4, pointing to E.
now, following the code, with your labelling:
[1] swap(v,0,2) [DACBE] -> [CADBE]
we've swapped our chosen value out and put it at the start of the array.
[2] last=0
this is a bit clever... we want to keep a counter so we know which values were greater and which less... you'll see how this progresses
[3] for (i=1;i<=4;i++)
for all the remaining elements in the list...
[4] if ((*comp)(v[i], 'C')<0)
if the value at i is LESS than 'C'...
[5] swap(v,++last,i);
put it at the start of the list!
let's run the code for 3,4,5
i=1:
[CADBE]
if ('A'<'C')
swap('A','A') (AND INCREMENT LAST!)
[CADBE]->[CADBE] (no change)
last=1
i=2:
[CADBE]
if ('D'<'C')
fails. move on.
i=3:
[CADBE]
if ('B'<'C')
swap('B','D') And increment last!
[CADBE] -> [CABDE] (lookit! it's sorting!)
last=2
i=4:
[CABDE]
if ('E'<'C')
fails. move on.
Ok, ace. so that loop gives is [CABDE], last=2 ('B')
Now line [6].... swap(left,last)... that's swap('C','B')
[CABDE] -> [BACDE]
Now, the magic of this is... it's partially sorted! BA < C < DE!
So now we sort the sub-lists!!
[7] -> [BA] -> [AB]
so
[BACDE] -> [ABCDE]
[8]-> [DE]->[DE]
so
[ABCDE] -> [ABCDE]
and we're done!
K&R's quick is an example of great coding but not a great example of how quicksort works. The purpose of the preswap is not intuitive and the identity swaps are inefficient and confusing. I have written a program to help clarify this. Code comments explain the issues.
I have compiled and tested only under Linux but Visual Studio should have no problem with this plain vanilla console app.
/***************************** QUICK.CPP ***************************************
Author: David McCracken
Updated: 2009-08-14
Purpose: This illustrates the operation of the quicksort in K&R "The C
Programming Language" (second edition p. 120). Many programmers are frustrated
when they attempt to understand quicksort in general from this version, which
was clearly not intended as a tutorial on quicksort but on the use of pointers
to functions. My program modifies the original to work only on ints in order to
focus on the sorting process. It can print the global list and recursive
sublist at each change to trace the sorting decision process. My program also
clarifies two confusing aspects, both involving unexplained swapping, of the
original by comparing its operation to that of two further modified versions.
One confusing thing that the original does is to swap an item with itself.
The code (modified for ints only) is:
last = left;
for( i = left+1 ; i <= right ; i++ )
if( v[i] < v[ left ] )
swap( v[ ++last ], v[ i ]);
Note that left and v[ left ] are loop-invariant. v[ left ] is the pivot.
A superfluous swap is performed on all values less than the pivot without an
earlier value greater than the pivot. For example, given sublist (after
preswap) 9,8,5,7,4,6, initially i = left + 1, selecting 8. Since this is less
than 9, last is incremented to point to the same element as i (selecting 8) and
a superfluous swap is performed. At the next iteration, last selects 8 while i
selects 5 and 5 is swapped with itself. This continues to the end of the
sublist. The sorting function krQuick2 is identical to krQuick but tests ++last
against i to avoid superfluous swapping. This certainly yields better
performance for practically no cost but, more importantly, helps to clarify
just what the code is trying to do, which is to find and swap a value that is
larger than the pivot with one that occurs later and is smaller than the pivot.
A second source of confusion is the purpose of the preswap, where the midpoint
value is swapped with the left-most. Since this is done without regard to
value, it cannot decrease entropy. In fact, it does exactly the opposite in the
very important case of a sublist that is already sorted, which normally makes
quicksort perform badly. This action deliberately unsorts a sorted list and
essentially does nothing to an unsorted one. This simple and cheap action
substantially improves average and worst case performance, as demonstrated by
the third variation, quick3, which just removes the preswap from krQuick2.
quick3 demonstrates that the preswap is not required; in fact that any value
can be chosen from the list to serve as the pivot. Only in the most unsorted
cases does quick3 exhibit slightly better performance than krQuick2 by virture
of skipping the preswap. With increasing initial order, the performance of
krQuick2 steadily improves over quick3.
Some confusion may also come from the testing of v[i] against v[left]. left and
v[ left ] are loop-invariant. An optimizing compiler should recognize this and
hoist the value out of the loop, but this loop-invariance is not immediately
obvious to someone studying this as an example of quicksort. During the swap
loop, v[left] serves only to hold the pivot value. An automatic could just as
easily hold the value and its purpose would be more clear. However, the code is
an example of indirection. We don't know what the list items are but we can be
sure that any one of them can fit into v[ left ] and that the swap function can
put it there. Thus, the one preswap operation does three things; it randomizes
a sorted sublist; it conveniently copies the pivot to a place where it won't be
subject to swapping; and it fills the hole in the loop left by extracting the
pivot. It does all of this without even knowing what the elements are and with
a function that we already have. This amazing programming feat is well worth
studying but not in the interest of understanding quicksort.
HOW TO USE THIS PROGRAM
There are three general variables, the function, the data to be sorted, and what
to display.
The simplified K&R original function, krQuick, is function 0. Function 1,
krQuick2, is krQuick with identity swaps removed. Function 2, quick3, is
krQuick2 without preswap.
The data to be sorted can be any one of five builtin lists or all of them or
a space-delimited list of decimal ints entered on the command line.
The displayed information affords a trace of the function's operation. In all
cases, the list is displayed before and after sorting, and executing statistics
are reported. If SHOW_NOTHING then nothing else is reported. If SHOW_GLOBAL,
the changing full list is displayed at each invocation of the recursive sort
function. If SHOW_LOCAL1, the sublist passed to the function is displayed before
it is modified. If SHOW_LOCAL, the sublist is displayed after each swap. If
SHOW_INDEX, the indices involved in swapping and the values at those indices
are shown before the swap occurs.These selections correspond to the SHOW_ enum
and are culmulative flags.
By default, all three functions are applied in succession to all five builtin
data lists, with SHOW_NOTHING. This is useful for comparing the performance of
the functions. For example, it shows that on the unordered list 11 0 10 1 9 2 8
3 7 4 6 5 quick3 uses 37 compares and 30 swaps while krQuick2 uses 38 compares
and 44 swaps. However, on the ordered list 0 1 2 3 4 5 6 7 8 9 10 11 quick3
uses 66 compares and 22 swaps while krQuick2 uses 25 compares and 28 swaps.
Command line arguments alter the content but not the order of operation. In all
cases, each selected function is applied to all selected data lists.
Command arguments are case-insensitive: F function selector, D data selector,
and S show what map. Each is followed without space by a single character.
F0, F1, F2, FA select function 0, 1, or 2 only or all functions.
D0, D1, D2, D3, D4, DA select builtin data list 0, 1, 2, 3, or 4 only or all.
S0 (default) shows no extra information.
S1 (GLOBAL) shows the full list (without "GLOBAL" legend) at each invocation.
S2 (LOCAL1) shows the sublist before processing.
S3 (GLOBAL+LOCAL1)
S4 (LOCAL) shows the sublist after each swap. It also shows the sublist before
processing.
S8 (INDEX) shows indices but these would never be shown without at least LOCAL,
which can't be combined with 8 in the single-digit argument.
SA (All)
Note that the Local legend includes a numeric suffix to identify where in the
point in the code that is reporting.
The most useful S formats are S1, S5, and SA (S0 is default).
After any F and S arguments, any space-delimited list of numbers will be taken
as the data list. Any D argument is ignored. For example:
quick 20 21 15 12 40 0
applies all three functions to the data, reporting only the unsorted and sorted
full lists and operational statistics.
quick f0 sa 20 21 15 12 40 0
applies only function 0 krQuick to the data, reporting everything.
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// ======================== DATA AND DECLARATIONS ===============================
#define DIM(A) ( sizeof A / sizeof A[0])
typedef unsigned int UINT;
enum { SHOW_NOTHING, SHOW_GLOBAL = 1, SHOW_LOCAL1 = 2, SHOW_LOCAL = 4,
SHOW_INDEX = 8, SHOW_ALL = 0xFF };
int showWhat = SHOW_NOTHING;
int list0[] = { 4,0,2,5,1,3 };
int list1[] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
int list2[] = { 11,10,9,8,7,6,5,4,3,2,1,0 };
int list3[] = { 11,9,7,5,3,1,0,2,4,6,8,10 };
int list4[] = { 11,0,10,1,9,2,8,3,7,4,6,5 };
static struct { int *list; int cnt; } lists[] =
{
{ list0, DIM( list0 )},
{ list1, DIM( list1 )},
{ list2, DIM( list2 )},
{ list3, DIM( list3 )},
{ list4, DIM( list4 )},
};
int total[ 1000 ];
int totalCnt;
int *userData = 0;
int userDataLen = 0;
int recursion; // Current recursion level.
int calls; // Number of times the sort function is called.
int depth; // Maximum recursion level.
int swaps; // Count of swaps.
int compares; // Count of list item compares.
int totCalls;
int totDepth;
int totCompares;
int totSwaps;
void (*sortFunc)( int *list, int left, int right );
char dArg = 'A'; // command line argument selects 0,1,2,3,4, or A
int dataList; // If dArg is numeric, this is its int value.
//============================== FUNCTIONS =====================================
// ------------------------------ indent --------------------------------------
// Print two spaces for each level of recursion to indent subsequent print
// output.
// ............................................................................
void indent( void )
{
for( int indent = 1 ; indent < recursion ; indent++ )
printf( " " );
}
// -------------------------------- show ---------------------------------------
// Print the given int list according to the global control setting showWhat
// and the given local identification. This may print nothing or the global
// list or the local sublist. It may or may not print the legend GLOBAL or
// LOCALx where x is the local ID, which tells at what point in the sort code
// we are showing the sublist.
// Returns: Nothing
// Arguments:
//- int *ia points to the int list.
//- int cnt is the number of elements in the list.
//- int local tells the local point in the sort routine if greater than 0. 0
// indicates that this is global. In either case, format is controlled by
// showWhat. If local is -1, the list is printed unconditionally and without
// any legend.
// Global:
//- showWhat bitmapped control word
//-- 0 (SHOW_NOTHING) This is the complete value, not a bit flag.
//-- 1 (SHOW_GLOBAL) Print the list if local is 0. If any other bit is also
// set, the GLOBAL legend is printed before the list.
//-- 2 (SHOW_LOCAL1) Print the list only if local is 1.
//-- 3 (SHOW_LOCAL) Print the list if local is 1 or greater.
//
// ...................... notes .............................................
// SHOW_NOTHING
// This exists because the callers don't test showWhat before calling. If we
// only want to show the initial unsorted list and final sorted version then
// SHOW_NOTHING blocks all print output from the sort function. The control
// function calls show with local = -1 to print the list.
// ..........................................................................
void show( int *ia, int cnt, int local )
{
if( local >= 0 )
{
switch( showWhat )
{
case SHOW_NOTHING:
return;
case SHOW_GLOBAL: // Only SHOW_GLOBAL
if( local > 0 )
return; // This is a local
break; // Print list without legend.
default: // Some combination of SHOW_GLOBAL, SHOW_LOCAL1, SHOW_LOCAL
if( local == 0 ) // global
{
if( ( showWhat & SHOW_GLOBAL ) == 0 )
return;
printf( "GLOBAL " );
}
else if( showWhat & SHOW_LOCAL || ( showWhat & SHOW_LOCAL1 && local == 1 ))
{
indent();
printf( "Local%d: ", local );
}
else
return;
}
}
for( int *end = ia + cnt ; ia < end ; ia++ )
printf( "%d ", *ia );
putchar( '\n' );
}
// -------------------------------- swap ---------------------------------------
void swap( int *p1, int *p2 )
{
int temp = *p2;
*p2 = *p1;
*p1 = temp;
++swaps;
}
// ------------------------------ krQuick --------------------------------------
// K&R's quick function modified to handle only integers and to use inline
// numeric comparison instead of an indirect comp function.
// .............................................................................
void krQuick( int *list, int left, int right )
{
int i, last;
++calls;
if( recursion > depth )
depth = recursion; // At first call recursion = 0 and depth is 0, i.e. no recursion yet.
++recursion;
show( total, totalCnt, 0 ); // GLOBAL
show( list + left, right - left + 1, 1 ); // LOCAL
if( left < right )
{
swap( list + left, list + (left + right) / 2 );
++swaps;
show( list + left, right - left + 1, 2 );
last = left;
for( i = left + 1 ; i <= right ; i++ )
{
++compares;
if( list[ i ] < list[ left ])
{
if( showWhat & SHOW_INDEX )
{
indent();
printf( "i=%d #i=%d left=%d #left=%d last=%d\n",
i, list[i], left, list[ left ], last );
}
swap( list + ++last, list + i );
show( list + left, right - left + 1, 3 );
++swaps;
}
}
swap( list + left, list + last );
show( list + left, right - left + 1, 4 );
++swaps;
krQuick( list, left, last - 1 );
krQuick( list, last + 1, right );
}
--recursion;
}
// ------------------------------- krQuick2 ------------------------------------
// K&R's quick function modified as in krQuick plus elimination of identity
// swaps.
// .............................................................................
void krQuick2( int *list, int left, int right )
{
int i, last;
++calls;
if( recursion > depth )
depth = recursion; // At first call recursion = 0 and depth is 0, i.e. no recursion yet.
++recursion;
show( total, totalCnt, 0 ); // GLOBAL
show( list + left, right - left + 1, 1 ); // LOCAL
if( left < right )
{
swap( list + left, list + (left + right) / 2 );
++swaps;
show( list + left, right - left + 1, 2 );
last = left;
for( i = left + 1 ; i <= right ; i++ )
{
++compares;
if( list[ i ] < list[ left ] && ++last != i )
{
if( showWhat & SHOW_INDEX )
{
indent();
printf( "i=%d #i=%d left=%d #left=%d last=%d\n",
i, list[i], left, list[ left ], last );
}
swap( list + last, list + i );
show( list + left, right - left + 1, 3 );
++swaps;
}
}
swap( list + left, list + last );
show( list + left, right - left + 1, 4 );
++swaps;
krQuick2( list, left, last - 1 );
krQuick2( list, last + 1, right );
}
--recursion;
}
// ------------------------------------ quick3 ---------------------------------
// krQuick2 modified to not do the preswap. In the K&R original, the purpose of
// the preswap is to introduce randomness into a presorted sublist. The sorting
// result is not changed by eliminating this but the performance degrades with
// more compares and swaps in all cases between average and worst. Only near the
// best case does eliminating the preswap improve performance.
// ............................................................................
void quick3( int *list, int left, int right )
{
int i, last;
++calls;
if( recursion > depth )
depth = recursion; // At first call recursion = 0 and depth is 0, i.e. no recursion yet.
++recursion;
show( total, totalCnt, 0 ); // GLOBAL
show( list + left, right - left + 1, 1 ); // LOCAL
if( left < right )
{
last = left;
for( i = left + 1 ; i <= right ; i++ )
{
++compares;
if( list[ i ] < list[ left ] && ++last != i )
{
if( showWhat & SHOW_INDEX )
{
indent();
printf( "i=%d #i=%d left=%d #left=%d last=%d\n",
i, list[i], left, list[ left ], last );
}
swap( list + last, list + i );
show( list + left, right - left + 1, 3 );
++swaps;
}
}
swap( list + left, list + last );
show( list + left, right - left + 1, 4 );
++swaps;
quick3( list, left, last - 1 );
quick3( list, last + 1, right );
}
--recursion;
}
static struct { void (*func)( int *list, int left, int right ) ; char *name ; } sortFuncs[] =
{
{ krQuick, (char*)"krQuick" },
{ krQuick2, (char*)"krQuick2 (no identity swaps)" },
{ quick3, (char*)"quick3 (no preswaps)" }
};
// ------------------------------------ sortOne --------------------------------
// Set up performance counters, invoke the currently selected sort on the current
// data list, and print the performance (for this one case of selected function
// applied to selected data list).
// .............................................................................
void sortOne( void )
{
recursion = 0;
calls = 0;
depth = 0;
swaps = 0;
compares = 0;
show( total, totalCnt, -1 );
sortFunc( total, 0, totalCnt - 1 );
show( total, totalCnt, -1 );
printf( "Calls = %d, depth = %d, compares = %d, swaps = %d\n",
calls, depth, compares, swaps );
printf( "---------------------------------\n" );
}
// ---------------------------- sortOneSet -------------------------------------
// Purpose: Apply the currently selected sort function to one data list.
void sortOneSet( int idx )
{
if( idx < 0 )
{
totalCnt = userDataLen;
memcpy( total, userData, totalCnt * sizeof( int ));
}
else
{
totalCnt = lists[ idx ].cnt;
memcpy( total, lists[ idx ].list, totalCnt * sizeof( int ));
}
sortOne();
totCalls += calls;
totDepth += depth;
totCompares += compares;
totSwaps += swaps;
}
// ------------------------- testOneFunc ---------------------------------------
// Purpose: Apply the selected function to one or all data lists.
// Returns: Nothing
// Arguments: int sel is 0,1,or 2, selecting krQuick, krQuick2, or quick3.
// Globals: char dArg is the data list selector command line argument. It is '0',
// '1', '2', or 'A'. 'A' selects all data lists. Otherwise, int dataList is the
// int value of dArg, which has already been translated for us by the command
// line processor.
// .............................................................................
void testOneFunc( int sel )
{
totCalls = 0;
totDepth = 0;
totCompares = 0;
totSwaps = 0;
sortFunc = sortFuncs[ sel ].func;
printf( "====== %s ======\n", sortFuncs[ sel ].name );
if( userDataLen != 0 )
sortOneSet( -1 );
else if( dArg == 'A' )
{
for( UINT idx = 0 ; idx < DIM( lists ) ; idx++ )
sortOneSet( idx );
printf( "Total: calls = %d, depth = %d, compares = %d, swaps = %d\n",
totCalls, totDepth, totCompares, totSwaps );
}
else
sortOneSet( dataList );
}
// --------------------------------- main --------------------------------------
// Purpose: Process command line arguments, set up show (print output) and data
// list selectors, and invoke testOneFunc either once for the selected function
// or for each of the three functions.
// .............................................................................
int main( int argc, char **argv )
{
char *cp;
char fArg = 'A'; // function selector 0,1,2,A
UINT idx;
showWhat = SHOW_NOTHING;
dArg = 'A';
for( int cnt = 1 ; cnt < argc ; cnt++ )
{
cp = argv[ cnt ];
switch( toupper( *cp ))
{
case 'F':
fArg = toupper( cp[1] );
break;
case 'D':
dArg = toupper( cp[1] );
if( dArg != 'A' )
{
dataList = dArg - '0';
if( dataList < 0 || dataList >= (int)DIM( lists ))
{
printf( "Error: bad data list selector %c\n", dArg );
return 1;
}
}
break;
case 'S': // show selector matches bit-mapped showWhat or N or A
++cp;
if( *cp != 0 || toupper( *cp ) != 'N' )
{
if( toupper( *cp ) == 'A' )
showWhat = SHOW_ALL;
else
showWhat = atoi( cp );
}
break;
default:
if( !isdigit( *cp ))
{
printf( "Error: There is no option %c\n", *cp );
return 1;
}
for( idx = 0 ; idx < DIM( total ) && cnt < argc ; idx++, cnt++ )
total[ idx ] = atoi( argv[ cnt ] );
userData = (int*)malloc( sizeof( int ) * idx );
if( userData == 0 )
{
printf( "Error: Unable to allocate memory for data list\n" );
return 2;
}
memcpy( userData, total, sizeof( int ) * idx );
userDataLen = idx;
}
}
switch( fArg )
{
case 'A':
for( UINT sfi = 0 ; sfi < DIM( sortFuncs ) ; sfi++ )
testOneFunc( sfi );
break;
case '0':
case '1':
case '2':
testOneFunc( fArg - '0' );
break;
default:
printf( "Error: bad function selector %c\n", fArg );
return 1;
}
return 0;
}
Results of quick
This uses all defaults, which is most useful for comparing the performance
of the three different functions.
====== krQuick ======
4 0 2 5 1 3
0 1 2 3 4 5
Calls = 7, depth = 2, compares = 8, swaps = 20
---------------------------------
0 1 2 3 4 5 6 7 8 9 10 11
0 1 2 3 4 5 6 7 8 9 10 11
Calls = 15, depth = 3, compares = 25, swaps = 48
---------------------------------
11 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 11
Calls = 17, depth = 5, compares = 30, swaps = 62
---------------------------------
11 9 7 5 3 1 0 2 4 6 8 10
0 1 2 3 4 5 6 7 8 9 10 11
Calls = 13, depth = 5, compares = 33, swaps = 56
---------------------------------
11 0 10 1 9 2 8 3 7 4 6 5
0 1 2 3 4 5 6 7 8 9 10 11
Calls = 15, depth = 6, compares = 38, swaps = 60
---------------------------------
Total: calls = 67, depth = 21, compares = 134, swaps = 246
====== krQuick2 (no identity swaps) ======
4 0 2 5 1 3
0 1 2 3 4 5
Calls = 7, depth = 2, compares = 8, swaps = 16
---------------------------------
0 1 2 3 4 5 6 7 8 9 10 11
0 1 2 3 4 5 6 7 8 9 10 11
Calls = 15, depth = 3, compares = 25, swaps = 28
---------------------------------
11 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 11
Calls = 17, depth = 5, compares = 30, swaps = 52
---------------------------------
11 9 7 5 3 1 0 2 4 6 8 10
0 1 2 3 4 5 6 7 8 9 10 11
Calls = 13, depth = 5, compares = 33, swaps = 46
---------------------------------
11 0 10 1 9 2 8 3 7 4 6 5
0 1 2 3 4 5 6 7 8 9 10 11
Calls = 15, depth = 6, compares = 38, swaps = 44
---------------------------------
Total: calls = 67, depth = 21, compares = 134, swaps = 186
====== quick3 (no preswaps) ======
4 0 2 5 1 3
0 1 2 3 4 5
Calls = 7, depth = 3, compares = 10, swaps = 10
---------------------------------
0 1 2 3 4 5 6 7 8 9 10 11
0 1 2 3 4 5 6 7 8 9 10 11
Calls = 23, depth = 11, compares = 66, swaps = 22
---------------------------------
11 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 11
Calls = 23, depth = 11, compares = 66, swaps = 22
---------------------------------
11 9 7 5 3 1 0 2 4 6 8 10
0 1 2 3 4 5 6 7 8 9 10 11
Calls = 15, depth = 7, compares = 46, swaps = 54
---------------------------------
11 0 10 1 9 2 8 3 7 4 6 5
0 1 2 3 4 5 6 7 8 9 10 11
Calls = 19, depth = 6, compares = 37, swaps = 30
---------------------------------
Total: calls = 87, depth = 38, compares = 225, swaps = 138
*******************************************************************************
Results of quick f0 s5 d1
S5 format is best for displaying how the sublist changes during sorting. Since
LOCAL is displayed only after a swap, superfluous identity swaps (many in this
example) are readily apparent.
====== krQuick ======
0 1 2 3 4 5 6 7 8 9 10 11
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1: 0 1 2 3 4 5 6 7 8 9 10 11
Local2: 5 1 2 3 4 0 6 7 8 9 10 11
Local3: 5 1 2 3 4 0 6 7 8 9 10 11
Local3: 5 1 2 3 4 0 6 7 8 9 10 11
Local3: 5 1 2 3 4 0 6 7 8 9 10 11
Local3: 5 1 2 3 4 0 6 7 8 9 10 11
Local3: 5 1 2 3 4 0 6 7 8 9 10 11
Local4: 0 1 2 3 4 5 6 7 8 9 10 11
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1: 0 1 2 3 4
Local2: 2 1 0 3 4
Local3: 2 1 0 3 4
Local3: 2 1 0 3 4
Local4: 0 1 2 3 4
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1: 0 1
Local2: 0 1
Local4: 0 1
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1:
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1: 1
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1: 3 4
Local2: 3 4
Local4: 3 4
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1:
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1: 4
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1: 6 7 8 9 10 11
Local2: 8 7 6 9 10 11
Local3: 8 7 6 9 10 11
Local3: 8 7 6 9 10 11
Local4: 6 7 8 9 10 11
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1: 6 7
Local2: 6 7
Local4: 6 7
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1:
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1: 7
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1: 9 10 11
Local2: 10 9 11
Local3: 10 9 11
Local4: 9 10 11
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1: 9
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1: 11
0 1 2 3 4 5 6 7 8 9 10 11
Calls = 15, depth = 3, compares = 25, swaps = 48
********************************************************************************
Results of quick f0 sa d1
This is the same as the previous example but shows the additional detail of index
values that lead to the swapping decision. However, the clutter tends to obscure
what is actually happening to the sublist.
====== krQuick ======
0 1 2 3 4 5 6 7 8 9 10 11
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1: 0 1 2 3 4 5 6 7 8 9 10 11
Local2: 5 1 2 3 4 0 6 7 8 9 10 11
i=1 #i=1 left=0 #left=5 last=0
Local3: 5 1 2 3 4 0 6 7 8 9 10 11
i=2 #i=2 left=0 #left=5 last=1
Local3: 5 1 2 3 4 0 6 7 8 9 10 11
i=3 #i=3 left=0 #left=5 last=2
Local3: 5 1 2 3 4 0 6 7 8 9 10 11
i=4 #i=4 left=0 #left=5 last=3
Local3: 5 1 2 3 4 0 6 7 8 9 10 11
i=5 #i=0 left=0 #left=5 last=4
Local3: 5 1 2 3 4 0 6 7 8 9 10 11
Local4: 0 1 2 3 4 5 6 7 8 9 10 11
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1: 0 1 2 3 4
Local2: 2 1 0 3 4
i=1 #i=1 left=0 #left=2 last=0
Local3: 2 1 0 3 4
i=2 #i=0 left=0 #left=2 last=1
Local3: 2 1 0 3 4
Local4: 0 1 2 3 4
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1: 0 1
Local2: 0 1
Local4: 0 1
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1:
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1: 1
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1: 3 4
Local2: 3 4
Local4: 3 4
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1:
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1: 4
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1: 6 7 8 9 10 11
Local2: 8 7 6 9 10 11
i=7 #i=7 left=6 #left=8 last=6
Local3: 8 7 6 9 10 11
i=8 #i=6 left=6 #left=8 last=7
Local3: 8 7 6 9 10 11
Local4: 6 7 8 9 10 11
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1: 6 7
Local2: 6 7
Local4: 6 7
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1:
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1: 7
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1: 9 10 11
Local2: 10 9 11
i=10 #i=9 left=9 #left=10 last=9
Local3: 10 9 11
Local4: 9 10 11
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1: 9
GLOBAL 0 1 2 3 4 5 6 7 8 9 10 11
Local1: 11
0 1 2 3 4 5 6 7 8 9 10 11
Calls = 15, depth = 3, compares = 25, swaps = 48
magic useful google keywords: QuickSort
e.g. google:how quicksort works turns up this explanation: http://www.angelfire.com/pq/jamesbarbetti/articles/sorting/001a_HowQuicksortWorks.htm among others.
Essentially, the code applies a variation of quicksort to the elements between the left and right boundaries specified.
For the lines you've identified:
swap the middle element with the first (left) one. it will become the "pivot".
keep track of the boundary between bigger and smaller elements. this is where the pivot belongs.
for every element after the first one,
if it's smaller than the pivot,
move it before the first bigger element.
move the pivot back into place.
recursively apply qsort to the elements before the pivot. (the smaller ones)
recursively apply qsort to the elements after the pivot. (the bigger ones)
Try applying the code yourself to a list of numbers, and see if it makes more sense then....
There's a bug in your code, the lines at the end:
qsort(v, left, last - 1); [7]
qsort(v, last + 1, right); [8]
should be:
qsort(v, left, last - 1, comp); [7]
qsort(v, last + 1, right, comp); [8]
Or am I missing something?
Furthermore, it's bad style to reuse names of the standard library, especially if the new function has a different signature than the one in the lib.
The function qsort of the standard library has this prototype:
void qsort(void *base, size_t nel, size_t width, int (*compar)(const void *, const void *));
If your program is a bit bigger (more than one object file) this can give interesting bugs. Imagine another module calling the standard qsort function but as you have redefined it, with a compatible signature, but with a different semantic, you get an unexpected bug.
Hi I did the example from page 87. May be someone will understand from this. But before going for this code, see quicksort
/**
* qsort.c
* Quick sort using recursion
*/
#include <stdio.h>
void qsort(int v[], int left, int right);
int main()
{
int v[] = {9, 3, 4, 6, 7, 3, 1};
qsort(v, 0, 6);
int i;
for (i = 0; i < 7; i++)
printf(" %d ", v[i]);
printf("\n");
return 0;
}
void qsort(int v[], int left, int right)
{
int i, last; /* last is pivot */
void swap(int v[], int i, int j);
if (left >= right)
return;
swap(v, left, (left + right) / 2); // swap mid element to front
last = left; // set this position as pivot
for (i = left + 1; i <= right; i++) {
/*loop through every other element
swap elements less than pivot i.e bigger to right, smaller to left
*/
if (v[i] < v[left])
swap(v, ++last, i); // when swapping lesser element, record
// where our pivot moves
/*
we don't swap elements that are bigger than pivot, and are to right.
However we swap elements those are less than pivot.
With ++pivot we are essentially going to find out, where our
pivot will fit to be at the position, where all the elements
before it are less than it and all after it greater.
*/
}
// swap left(our pivot) to last(where pivot must go
// i.e all elements before pivot are less than it
// and all elements above it are greater
// remember they are lesser and greater
// but may not be sorted still
// this is called partition
swap(v, left, last);
// Do same(qsort) for all the elements before our pivot
// and above our pivot
qsort(v, left, last - 1); // last is our pivot position
qsort(v, last + 1, right);
// Each of above two qsort will use middle element as pivot and do
// what we did above, because same code will be executed by recursive
// functions
}
void swap(int v[], int i, int j)
{
int temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
The most important part is the pivot(put your one feet at place, while free to move other). We choose the middle element as pivot, bring it to front, compare it with all other elements. If they are less than our pivot we swap them and increment only our pivot position (be careful our pivot element still lies at first). After we finish the loop we bring the pivot element(which is at first) to this place (pivot place). After the loop, we have all the elements before pivot less than pivot and all those above pivot greater than pivot. At first loop they are not sorted. So you must again apply same sorting algorithm recursively to all elements below pivot and above pivot to sort them.

Resources