SlideShare uma empresa Scribd logo
1 de 9
Quick Sort
Dr. G. Jasmine Beulah,
Assistant Professor, Kristu Jayanti College,
Bengaluru
What is Quick Sort
• Most widely used sorting algorithm
• It follows Dive and Conquer paradigm
• Recursion is used in quicksort implementation
• In each recursive call, a pivot is chosen then the array is partitioned in
such a way that all the elements less than pivot lie to the left and all
the elements greater than pivot lie to the right.
• After every call, the chosen pivot occupies its correct position in the
array which it is supposed to as in sorted array
• So with each step, our problem gets reduced by 2 which leads to quick
sorting
• Pivot can be last element of current array, first element of current array
or any random element.
//program to sort a list of N integers using quicksort
#include<stdio.h>
void main()
{
int i,n,a[50];
void quicksort(int a[],int,int);
clrscr();
printf("nEnter the size of the array : ");
scanf("%d",&n);
printf("Enter the elements of the arrynt");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("nThe entered unsorted arrayn");
for(i=0;i<n;i++)
{
printf("t%d",a[i]);
}
quicksort(a,0,n-1);
printf("nSorted Arrayn");
for(i=0;i<n;i++)
{
printf("t%d",a[i]);
}
getch();
}
void quicksort(int a[],int low,int high)
{
int pivot,down,up,key,temp;
if(low<high)
{
down=low;
pivot=low;
up=high;
key=a[pivot];
while(down<up)
{
while((a[down]<=key)&&(down<=high))
down++;
while((a[up]>key)&&(up>=low))
up--;
if(down<up)
{
temp=a[up];
a[up]=a[down];
a[down]=temp;
}
}
temp=a[up];
a[up]=a[pivot];
a[pivot]=temp;
quicksort(a,low,up-1);
quicksort(a,up+1,high);
}
return;
}
Algorithm for mainegg
A is an array of n elements.
Step 1: Start
Step 2: Read the size of the array into n
Step 3: Read the array elements
Step 4: Print unsorted list
Step 5: Call the function quicksort(a,0,n-1)
Step 6: Print the sorted list
Step 7 : Stop
Algorithm for Quicksort
Quicksort(a[],lb, ub)
Step1: if( lb<ub)
up=lb; down=ub; key=a[lb];
Step 2: while(lb<=ub)
Step 3: while(a[up] < key) up++;
[End of while]
Step 4: while(a[down] > key) down--;
[End of while]
Step 5 : if(up<down)
[Interchange a[up] with a[down]]
temp=a[up]; a[up]=a[down]; a[down]=temp;
[End of if]
[End of while loop in step 2]
Step 6: if(up>down)
[Interchange the key element a[lb] with a[down]]
temp=a[lb];a[lb]=a[down];a[down]=temp;
[End of if]
Step 7: Call quicksort(a, lb, down-1);
Step 8: Call quicksort(a, down+1,ub);
[End of if in step 1]

Mais conteúdo relacionado

Semelhante a Quick Sort.pptx

data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxMohammed472103
 
Java Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfJava Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfNiravPanchal50
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures LavanyaJ28
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfAkashSingh625550
 
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structuresSearching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structuresPRIANKA R
 
Dissertation Prsentation - Vaibhav
Dissertation Prsentation - VaibhavDissertation Prsentation - Vaibhav
Dissertation Prsentation - VaibhavVaibhav Dhattarwal
 
Machine learning Module-2, 6th Semester Elective
Machine learning Module-2, 6th Semester ElectiveMachine learning Module-2, 6th Semester Elective
Machine learning Module-2, 6th Semester ElectiveMayuraD1
 
VCE Unit 01 (1).pptx
VCE Unit 01 (1).pptxVCE Unit 01 (1).pptx
VCE Unit 01 (1).pptxskilljiolms
 
A Survey of Adaptive QuickSort Algorithms
A Survey of Adaptive QuickSort AlgorithmsA Survey of Adaptive QuickSort Algorithms
A Survey of Adaptive QuickSort AlgorithmsCSCJournals
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptxEpsiba1
 
Core java concepts
Core    java  conceptsCore    java  concepts
Core java conceptsChikugehlot
 

Semelhante a Quick Sort.pptx (20)

data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptx
 
Quick sort
Quick sortQuick sort
Quick sort
 
Java Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfJava Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdf
 
Quick sort
Quick sortQuick sort
Quick sort
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
 
Data Structures 2
Data Structures 2Data Structures 2
Data Structures 2
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
 
Searching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structuresSearching and sorting Techniques in Data structures
Searching and sorting Techniques in Data structures
 
Sorting
SortingSorting
Sorting
 
Sorting
SortingSorting
Sorting
 
Quick sort algorithm
Quick sort algorithmQuick sort algorithm
Quick sort algorithm
 
Dissertation Prsentation - Vaibhav
Dissertation Prsentation - VaibhavDissertation Prsentation - Vaibhav
Dissertation Prsentation - Vaibhav
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
Machine learning Module-2, 6th Semester Elective
Machine learning Module-2, 6th Semester ElectiveMachine learning Module-2, 6th Semester Elective
Machine learning Module-2, 6th Semester Elective
 
Queues
QueuesQueues
Queues
 
VCE Unit 01 (1).pptx
VCE Unit 01 (1).pptxVCE Unit 01 (1).pptx
VCE Unit 01 (1).pptx
 
A Survey of Adaptive QuickSort Algorithms
A Survey of Adaptive QuickSort AlgorithmsA Survey of Adaptive QuickSort Algorithms
A Survey of Adaptive QuickSort Algorithms
 
Keywords and classes
Keywords and classesKeywords and classes
Keywords and classes
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 
Core java concepts
Core    java  conceptsCore    java  concepts
Core java concepts
 

Mais de Dr. Jasmine Beulah Gnanadurai

Mais de Dr. Jasmine Beulah Gnanadurai (20)

Data Warehouse_Architecture.pptx
Data Warehouse_Architecture.pptxData Warehouse_Architecture.pptx
Data Warehouse_Architecture.pptx
 
DMQL(Data Mining Query Language).pptx
DMQL(Data Mining Query Language).pptxDMQL(Data Mining Query Language).pptx
DMQL(Data Mining Query Language).pptx
 
Stacks.pptx
Stacks.pptxStacks.pptx
Stacks.pptx
 
KBS Architecture.pptx
KBS Architecture.pptxKBS Architecture.pptx
KBS Architecture.pptx
 
Knowledge Representation in AI.pptx
Knowledge Representation in AI.pptxKnowledge Representation in AI.pptx
Knowledge Representation in AI.pptx
 
File allocation methods (1)
File allocation methods (1)File allocation methods (1)
File allocation methods (1)
 
Segmentation in operating systems
Segmentation in operating systemsSegmentation in operating systems
Segmentation in operating systems
 
Mem mgt
Mem mgtMem mgt
Mem mgt
 
Decision tree
Decision treeDecision tree
Decision tree
 
Association rules apriori algorithm
Association rules   apriori algorithmAssociation rules   apriori algorithm
Association rules apriori algorithm
 
Big data architecture
Big data architectureBig data architecture
Big data architecture
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representation
 
Aritificial intelligence
Aritificial intelligenceAritificial intelligence
Aritificial intelligence
 
Java threads
Java threadsJava threads
Java threads
 
Java Applets
Java AppletsJava Applets
Java Applets
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
 
JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript Functions
 
JavaScript Operators
JavaScript OperatorsJavaScript Operators
JavaScript Operators
 
Css Text Formatting
Css Text FormattingCss Text Formatting
Css Text Formatting
 
CSS - Cascading Style Sheet
CSS - Cascading Style SheetCSS - Cascading Style Sheet
CSS - Cascading Style Sheet
 

Último

Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 

Último (20)

Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 

Quick Sort.pptx

  • 1. Quick Sort Dr. G. Jasmine Beulah, Assistant Professor, Kristu Jayanti College, Bengaluru
  • 2. What is Quick Sort • Most widely used sorting algorithm • It follows Dive and Conquer paradigm • Recursion is used in quicksort implementation • In each recursive call, a pivot is chosen then the array is partitioned in such a way that all the elements less than pivot lie to the left and all the elements greater than pivot lie to the right. • After every call, the chosen pivot occupies its correct position in the array which it is supposed to as in sorted array • So with each step, our problem gets reduced by 2 which leads to quick sorting • Pivot can be last element of current array, first element of current array or any random element.
  • 3. //program to sort a list of N integers using quicksort #include<stdio.h> void main() { int i,n,a[50]; void quicksort(int a[],int,int); clrscr(); printf("nEnter the size of the array : "); scanf("%d",&n); printf("Enter the elements of the arrynt"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("nThe entered unsorted arrayn");
  • 5. void quicksort(int a[],int low,int high) { int pivot,down,up,key,temp; if(low<high) { down=low; pivot=low; up=high; key=a[pivot]; while(down<up) { while((a[down]<=key)&&(down<=high)) down++; while((a[up]>key)&&(up>=low)) up--;
  • 7. Algorithm for mainegg A is an array of n elements. Step 1: Start Step 2: Read the size of the array into n Step 3: Read the array elements Step 4: Print unsorted list Step 5: Call the function quicksort(a,0,n-1) Step 6: Print the sorted list Step 7 : Stop
  • 8. Algorithm for Quicksort Quicksort(a[],lb, ub) Step1: if( lb<ub) up=lb; down=ub; key=a[lb]; Step 2: while(lb<=ub) Step 3: while(a[up] < key) up++; [End of while] Step 4: while(a[down] > key) down--; [End of while] Step 5 : if(up<down) [Interchange a[up] with a[down]] temp=a[up]; a[up]=a[down]; a[down]=temp; [End of if] [End of while loop in step 2]
  • 9. Step 6: if(up>down) [Interchange the key element a[lb] with a[down]] temp=a[lb];a[lb]=a[down];a[down]=temp; [End of if] Step 7: Call quicksort(a, lb, down-1); Step 8: Call quicksort(a, down+1,ub); [End of if in step 1]