SlideShare a Scribd company logo
2
Most read
3
Most read
5
Most read
Quick Sorting
IntroductionIntroduction
 Quick sort is an example of divide and conquer algorithm. It
was developed in1960 by Tony Hoare. It is a recursive
algorithm.The intutive idea is as follows:
 Divide:- Choose any element of the list, say the last element
as a pivot value. Remove the pivot from the list. Divide the
remaining elements into two parts,left and right,such that all
the elements equal to or less than the pivot are in the left list,
while the elements greater than the pivot are in right list.
 Conquer:-Sort the two halves recursively using quick sort.
 Combine:-Return sorted_left + pivot + sorted_right,as your
final sorted_list.
History of Quick Sort AlgorithmHistory of Quick Sort Algorithm
 While studying at Moscow State University, Tony Hoare
received an offer of employment from
 National Physical Laboratory (NPL) to work on new project
for machine translation form Russian to English. However
,because dictionaries were sorted into alphabetical order
before translation. Hoare thought of two methods to solve
this problem.The first method would have taken an amount
of time proportional to the square of the length of a
sentence. The second method would later manifest as
quicksort.
 During this course, Hoare programmed an ultra-fast sorting
algorithm now known as quicksort. His paper on Quicksort
was also published in 1961,with another following in 1962.
Example of Quick SortExample of Quick Sort
Implementation of code in codeblocksImplementation of code in codeblocks
 #include < stdio. h>
 void quicksort(int [10],int,int);
 int main(){
 int x[20], size , i;
 printf("Enter size of the array: ");
 scanf("%d",&size);
 printf("Enter %d elements: ",size);
 for(i=0;i<size ; i++)
 scanf("% d",& x [i]);
 quicksort(x,0,size-1);
 return 0;
 }
 printf("Sorted elements: ");
 for(i=0;i<size ; i++)
 printf(" %d ", x[ i]);
 void quicksort(int x[10],int first,int last){
 int pivot,j,temp,i;
 if(first<last){
 pivot=first;
 i=first;
 j=last;
 while(i<j){
 while(x[i]<=x[pivot]&&i<last)
Algorithm For Quick SortAlgorithm For Quick Sort
 Quick( A, N, Beg, End , Loc)
 Here A is an array of N elements .Parameters Beg and End contain
the boundary values of the sublist to which this procedure applies.
Loc keeps track of the position of the first element A[Beg] of the
sublist during the procedure. The local variable Left and Right will
contain the boundary values of the list of elements that have not
been scanned.
1. [Initialize].Set Left:= Beg, Right:=End and Loc:=Beg.
2. [Scan from right to left]
a) Repeat while A[Loc]<=A[Right] and Loc!=Right. Right:=Right-1
[End of Loop]
b) If Loc=Right, then: return.
c) If A[Loc]>A[Right],then:
i. Interchange A[loc] and A[Right].
ii. Set Loc:=Right.
iii. Go to Step 3.
3. [Scan from left to right]
a) Repeat while A[Left]<=A[Loc] and Left!=Loc: Left :=Left+1.
b) If Loc=Left , then return.
c) if A[Left]>A[Loc],then
i. Interchange A[Left] and A[Loc].
ii. Set Loc:=Left
iii. Go to Step 2.
[End of if structure].
 i++;
 while(x[j]>x[pivot])
 j--;
 if(i<j){
 temp=x[i];
 x[i]=x[j];
 x[j]=temp;}
 }
 temp=x[pivot];
 x[pivot]=x[j];
 x[j]=temp;
 quicksort(x,first,j-1);
 quicksort(x,j+1,last);
 }
 }
ConclusionConclusion
We discussed the history of the algorithm. Then implemented
the code in codeblocks. Quick sort is an efficient algorithm and
has an average running time of O(n*log(n)).While the worst-case
running time for quick sort is higher than that of the merge sort,
in practice quick sort works slightly faster the the merge sort.

More Related Content

PPTX
Binary search
PDF
Algorithms - Aaron Bloomfield
PDF
Linear search
PPTX
Insertion sort
PDF
Quick sort
DOCX
Best,worst,average case .17581556 045
PDF
Sorting Algorithms
PPTX
Quick and radix sort
Binary search
Algorithms - Aaron Bloomfield
Linear search
Insertion sort
Quick sort
Best,worst,average case .17581556 045
Sorting Algorithms
Quick and radix sort

What's hot (20)

PDF
Algorithms Lecture 6: Searching Algorithms
PPTX
Quick Sort
PPTX
Algorithm & data structures lec4&5
PDF
Data structures and algorithms - sorting algorithms
PPT
Sorting Algorithms
PPT
Searching algorithms
PPTX
Insertion sort
PPT
Data Structures 2
PPTX
Different types of Shoring Algorithms with Animation
PPTX
Dsa – data structure and algorithms searching
PDF
Linear search algorithm
PDF
PPTX
Review of basic data structures
PPT
Lect11 Sorting
PDF
Quick Select (Decrease and Conquer)
PPT
L10 sorting-searching
PPT
Binary Search
PPTX
Vectors,squence & list
PDF
Algorithms Lecture 6: Searching Algorithms
Quick Sort
Algorithm & data structures lec4&5
Data structures and algorithms - sorting algorithms
Sorting Algorithms
Searching algorithms
Insertion sort
Data Structures 2
Different types of Shoring Algorithms with Animation
Dsa – data structure and algorithms searching
Linear search algorithm
Review of basic data structures
Lect11 Sorting
Quick Select (Decrease and Conquer)
L10 sorting-searching
Binary Search
Vectors,squence & list
Ad

Similar to Quick sorting (20)

PPTX
Quick sort
PPTX
Divide and conquer - Quick sort
PPTX
Quick-Sort Algorithm and pivot selection
PPTX
Unit 2 - Quick Sort.pptx
PPTX
09 QUICK SORT Design and Analysis of algorithms
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
PPTX
Weak 11-12 Sorting update.pptxbhjiiuuuuu
PPTX
Sorting
PPT
quicksort (1).ppt
PPTX
Divide-and-conquer
PPTX
PPTX
data structures and algorithms Unit 3
PPT
quick_sort_with_explanationandImplmentation.ppt
PPTX
Sortings .pptx
PDF
Quick Sort By Prof Lili Saghafi
PDF
Sorting Algorithms and their implementations
PPTX
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
PPTX
Quicksort algorithm and implantation process
PPT
Unit 7 sorting
Quick sort
Divide and conquer - Quick sort
Quick-Sort Algorithm and pivot selection
Unit 2 - Quick Sort.pptx
09 QUICK SORT Design and Analysis of algorithms
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
Weak 11-12 Sorting update.pptxbhjiiuuuuu
Sorting
quicksort (1).ppt
Divide-and-conquer
data structures and algorithms Unit 3
quick_sort_with_explanationandImplmentation.ppt
Sortings .pptx
Quick Sort By Prof Lili Saghafi
Sorting Algorithms and their implementations
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
Quicksort algorithm and implantation process
Unit 7 sorting
Ad

More from Lovely Professional University (20)

PPTX
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
PPTX
Effort Estimation: Meaning, Problems with Estimation, Basis, Estimation Techn...
PPTX
Project Approach: Intro. Technical Plan, Choice of Process Models: Waterfall,...
PPTX
Programme Management & Project Evaluation
PPTX
Step Wise Project Planning: Project Scope, Objectives, Infrastructure, Charac...
PPTX
Introduction to Software Project Management:
PDF
The HyperText Markup Language or HTML is the standard markup language
PPTX
PPTX
PPTX
Getting Input from User
PPTX
PPTX
Transaction Processing in DBMS.pptx
PPT
web_server_browser.ppt
PPTX
PPTX
Number System.pptx
PPT
Programming Language.ppt
PPTX
Information System.pptx
PPTX
Applications of Computer Science in Pharmacy-1.pptx
PPTX
Application of Computers in Pharmacy.pptx
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Effort Estimation: Meaning, Problems with Estimation, Basis, Estimation Techn...
Project Approach: Intro. Technical Plan, Choice of Process Models: Waterfall,...
Programme Management & Project Evaluation
Step Wise Project Planning: Project Scope, Objectives, Infrastructure, Charac...
Introduction to Software Project Management:
The HyperText Markup Language or HTML is the standard markup language
Getting Input from User
Transaction Processing in DBMS.pptx
web_server_browser.ppt
Number System.pptx
Programming Language.ppt
Information System.pptx
Applications of Computer Science in Pharmacy-1.pptx
Application of Computers in Pharmacy.pptx

Recently uploaded (20)

PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
OOP with Java - Java Introduction (Basics)
PPT
Project quality management in manufacturing
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Safety Seminar civil to be ensured for safe working.
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
additive manufacturing of ss316l using mig welding
PDF
737-MAX_SRG.pdf student reference guides
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PDF
Well-logging-methods_new................
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
CYBER-CRIMES AND SECURITY A guide to understanding
Model Code of Practice - Construction Work - 21102022 .pdf
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
OOP with Java - Java Introduction (Basics)
Project quality management in manufacturing
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Automation-in-Manufacturing-Chapter-Introduction.pdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Safety Seminar civil to be ensured for safe working.
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
additive manufacturing of ss316l using mig welding
737-MAX_SRG.pdf student reference guides
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Fundamentals of safety and accident prevention -final (1).pptx
R24 SURVEYING LAB MANUAL for civil enggi
III.4.1.2_The_Space_Environment.p pdffdf
Well-logging-methods_new................

Quick sorting

  • 2. IntroductionIntroduction  Quick sort is an example of divide and conquer algorithm. It was developed in1960 by Tony Hoare. It is a recursive algorithm.The intutive idea is as follows:  Divide:- Choose any element of the list, say the last element as a pivot value. Remove the pivot from the list. Divide the remaining elements into two parts,left and right,such that all the elements equal to or less than the pivot are in the left list, while the elements greater than the pivot are in right list.  Conquer:-Sort the two halves recursively using quick sort.  Combine:-Return sorted_left + pivot + sorted_right,as your final sorted_list.
  • 3. History of Quick Sort AlgorithmHistory of Quick Sort Algorithm  While studying at Moscow State University, Tony Hoare received an offer of employment from  National Physical Laboratory (NPL) to work on new project for machine translation form Russian to English. However ,because dictionaries were sorted into alphabetical order before translation. Hoare thought of two methods to solve this problem.The first method would have taken an amount of time proportional to the square of the length of a sentence. The second method would later manifest as quicksort.  During this course, Hoare programmed an ultra-fast sorting algorithm now known as quicksort. His paper on Quicksort was also published in 1961,with another following in 1962.
  • 4. Example of Quick SortExample of Quick Sort
  • 5. Implementation of code in codeblocksImplementation of code in codeblocks  #include < stdio. h>  void quicksort(int [10],int,int);  int main(){  int x[20], size , i;  printf("Enter size of the array: ");  scanf("%d",&size);  printf("Enter %d elements: ",size);  for(i=0;i<size ; i++)  scanf("% d",& x [i]);  quicksort(x,0,size-1);
  • 6.  return 0;  }  printf("Sorted elements: ");  for(i=0;i<size ; i++)  printf(" %d ", x[ i]);  void quicksort(int x[10],int first,int last){  int pivot,j,temp,i;  if(first<last){  pivot=first;  i=first;  j=last;  while(i<j){  while(x[i]<=x[pivot]&&i<last)
  • 7. Algorithm For Quick SortAlgorithm For Quick Sort  Quick( A, N, Beg, End , Loc)  Here A is an array of N elements .Parameters Beg and End contain the boundary values of the sublist to which this procedure applies. Loc keeps track of the position of the first element A[Beg] of the sublist during the procedure. The local variable Left and Right will contain the boundary values of the list of elements that have not been scanned. 1. [Initialize].Set Left:= Beg, Right:=End and Loc:=Beg. 2. [Scan from right to left] a) Repeat while A[Loc]<=A[Right] and Loc!=Right. Right:=Right-1 [End of Loop] b) If Loc=Right, then: return. c) If A[Loc]>A[Right],then: i. Interchange A[loc] and A[Right]. ii. Set Loc:=Right. iii. Go to Step 3.
  • 8. 3. [Scan from left to right] a) Repeat while A[Left]<=A[Loc] and Left!=Loc: Left :=Left+1. b) If Loc=Left , then return. c) if A[Left]>A[Loc],then i. Interchange A[Left] and A[Loc]. ii. Set Loc:=Left iii. Go to Step 2. [End of if structure].
  • 9.  i++;  while(x[j]>x[pivot])  j--;  if(i<j){  temp=x[i];  x[i]=x[j];  x[j]=temp;}  }  temp=x[pivot];  x[pivot]=x[j];  x[j]=temp;  quicksort(x,first,j-1);  quicksort(x,j+1,last);  }  }
  • 10. ConclusionConclusion We discussed the history of the algorithm. Then implemented the code in codeblocks. Quick sort is an efficient algorithm and has an average running time of O(n*log(n)).While the worst-case running time for quick sort is higher than that of the merge sort, in practice quick sort works slightly faster the the merge sort.