• Basic Fundamentals
•Introduction to Quick Sort
• Steps to perform Quick Sort
• Algorithm and pseudocode for Quick Sort
• Analysis of Quick Sort
COURSE OUTLINE
3.
• Array isa data structure where all elements are arranged sequentially.
Array
• It is a collection of elements of same data type.
• The base value is index 0
• To access elemnts :
variable_name[ index of element ]
4.
Recursio
n
• The processin which a function calls itself is called recursion.
• Through Recursion, we can reduce the length of our code and make it easier to rea
and write.
• For example factorial of a number.
Suppose 5! = 5*4*3*2*1 which is same as 5*4!
int fact(int n)
{
if (n < = 1) // base case(solution to base case
is provided)
return 1;
else
return n*fact(n-1);
}
5.
INTRODUCTION TO
SORTING
• Sortingis the process of arranging elements of an array in non increasing or
non decreasing order.
• Example of Sorting:
Consider an array of numbers:
A={5, 2, 8, 1, 3}
After sorting in non increasing order:
A={1, 2, 3, 5, 8}
After sorting in non decreasing order:
A={8, 5, 3, 2, 1}
6.
INTRODUCTION TO QUICKSORT ALGORITHM
• QuickSort is fastest sorting algorithm
• It is a divide-and-conquer algorithm that makes it easier to
solve problems.
• It is efficient on large data sets.
7.
DIVIDE AND CONQUER
Consistsof the Following 3 Steps:
• Divide: Dividing the Problem Into Smaller Sub-Problems.
• Conquer: Solve Sub-Problems by Calling Recursively.
• Combine: Combine the Sub-Problems to Get the Final Solution of the Whole
Problem.
8.
Steps To performQuicksort:
Choose a Pivot
• There are no such rules for
choosing a pivot.
Place the pivot in its correct
position.
Partition the elements into two
sections
• Elements <= pivot in left side
• Elements > pivot in right side
Recursively Quicksort the two
halves until down to one element
STEPS OF QUICK SORT
ALGORITHM
9.
ALGORITHM
QuickSort ( arr,low, high )
{
If ( low < high )
{
pivotindex = Partition ( arr, low, high )
QuickSort ( arr, low, pivotindex-1 )
QuickSort ( arr, pivotindex+1, high)
}
}
10.
ALGORITHM
QuickSort ( arr,low, high )
{
If ( low < high )
{
pivotindex = Partition ( arr, low, high )
QuickSort ( arr, low, pivotindex-1 )
QuickSort ( arr, pivotindex+1, high)
}
}
Partition ( arr, low, high )
{
pivot = arr[0];
i = low;
j = high;
while ( i < j )
{
while (arr[ i ] <= pivot && i <= high-1)
{
i++;
}
while (arr[ j ] > pivot && i >= low+1)
{
j--;
}
If (i<j)
swap (arr[ i ], arr[ j ] );
}
swap (arr[ low ], arr[ j ]);
return(j);
}