SlideShare a Scribd company logo
1 of 4
Download to read offline
/*** program of quick sort ****
* http://www.tutorial4us.com/data-structure/c-quick-sort */
#include<stdio.h>
#include<conio.h>
void quicksort(int [],int,int);
int partition(int [],int,int);
main()
{
int a[20],p,q,i,n;
clrscr();
printf("How many elements you want to enter: ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("nEnter any %d elements: "n);
scanf("%d",&a[i]);
}
p=0;
q=n-1;
printf("nnnArray Befor Sorting : ");
for(i=0; i<n; i++)
{
delay(500);
printf("%5d",a[i]);
}
quicksort(a,p,q);
printf("nnnArray After Sorting : ");
for(i=0; i<n; i++)
{
delay(500);
printf("%5d",a[i]);
}
getch();
return 0;
}
void quicksort(int a[],int p,int q)
{
int j;
if(p<q)
{
j=partition(a,p,q+1);
quicksort(a,p,j-1);
quicksort(a,j+1,q);
}
}
int partition(int a[],int m,int p)
{
int v,i,j;
int temp;
v=a[m];
i=m;j=p;
do
{
do
{
i += 1;
}
while(a[i]<v);
do
{
j -= 1;
}
while(a[j]>v);
if(i<j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
while(i<j);
a[m] =a[j];
a[j] = v;
return j;
}

More Related Content

What's hot

Bcsl 033 data and file structures lab s4-3
Bcsl 033 data and file structures lab s4-3Bcsl 033 data and file structures lab s4-3
Bcsl 033 data and file structures lab s4-3Dr. Loganathan R
 
C언어 스터디 강의자료 - 3차시
C언어 스터디 강의자료 - 3차시C언어 스터디 강의자료 - 3차시
C언어 스터디 강의자료 - 3차시Junha Jang
 
C언어 스터디 강의자료 - 4차시
C언어 스터디 강의자료 - 4차시C언어 스터디 강의자료 - 4차시
C언어 스터디 강의자료 - 4차시Junha Jang
 
C언어 스터디 강의자료 - 2차시
C언어 스터디 강의자료 - 2차시C언어 스터디 강의자료 - 2차시
C언어 스터디 강의자료 - 2차시Junha Jang
 
Bcsl 033 data and file structures lab s1-2
Bcsl 033 data and file structures lab s1-2Bcsl 033 data and file structures lab s1-2
Bcsl 033 data and file structures lab s1-2Dr. Loganathan R
 
Pruebabfs
PruebabfsPruebabfs
PruebabfsJorge
 
[程式設計]標準差
[程式設計]標準差[程式設計]標準差
[程式設計]標準差Hui-Shih Leng
 
Tablas hash (Rubén Ndong Obiang)
Tablas hash (Rubén Ndong Obiang)Tablas hash (Rubén Ndong Obiang)
Tablas hash (Rubén Ndong Obiang)edi.euitio
 
Bai lam chuong 3
Bai lam chuong 3Bai lam chuong 3
Bai lam chuong 3tran thai
 
jQuery入門 - 芸大 Webデザイン演習B
jQuery入門 - 芸大 Webデザイン演習BjQuery入門 - 芸大 Webデザイン演習B
jQuery入門 - 芸大 Webデザイン演習BAtsushi Tadokoro
 
Yohan jacobi gaussseidel_analisis
Yohan jacobi gaussseidel_analisisYohan jacobi gaussseidel_analisis
Yohan jacobi gaussseidel_analisisYohan Sidik
 

What's hot (20)

07 3 do-while반복문
07 3 do-while반복문07 3 do-while반복문
07 3 do-while반복문
 
Cpro
CproCpro
Cpro
 
Bcsl 033 data and file structures lab s4-3
Bcsl 033 data and file structures lab s4-3Bcsl 033 data and file structures lab s4-3
Bcsl 033 data and file structures lab s4-3
 
C언어 스터디 강의자료 - 3차시
C언어 스터디 강의자료 - 3차시C언어 스터디 강의자료 - 3차시
C언어 스터디 강의자료 - 3차시
 
C언어 스터디 강의자료 - 4차시
C언어 스터디 강의자료 - 4차시C언어 스터디 강의자료 - 4차시
C언어 스터디 강의자료 - 4차시
 
C언어 스터디 강의자료 - 2차시
C언어 스터디 강의자료 - 2차시C언어 스터디 강의자료 - 2차시
C언어 스터디 강의자료 - 2차시
 
Union
UnionUnion
Union
 
Alocação Dinâmica em C
Alocação Dinâmica em CAlocação Dinâmica em C
Alocação Dinâmica em C
 
Aman
AmanAman
Aman
 
Analytics code
Analytics codeAnalytics code
Analytics code
 
Bcsl 033 data and file structures lab s1-2
Bcsl 033 data and file structures lab s1-2Bcsl 033 data and file structures lab s1-2
Bcsl 033 data and file structures lab s1-2
 
Pruebabfs
PruebabfsPruebabfs
Pruebabfs
 
[程式設計]標準差
[程式設計]標準差[程式設計]標準差
[程式設計]標準差
 
Tablas hash (Rubén Ndong Obiang)
Tablas hash (Rubén Ndong Obiang)Tablas hash (Rubén Ndong Obiang)
Tablas hash (Rubén Ndong Obiang)
 
Multiplicacion de matrices
Multiplicacion de matricesMultiplicacion de matrices
Multiplicacion de matrices
 
N primo clase programa
N primo clase programaN primo clase programa
N primo clase programa
 
Bai lam chuong 3
Bai lam chuong 3Bai lam chuong 3
Bai lam chuong 3
 
Pilas
PilasPilas
Pilas
 
jQuery入門 - 芸大 Webデザイン演習B
jQuery入門 - 芸大 Webデザイン演習BjQuery入門 - 芸大 Webデザイン演習B
jQuery入門 - 芸大 Webデザイン演習B
 
Yohan jacobi gaussseidel_analisis
Yohan jacobi gaussseidel_analisisYohan jacobi gaussseidel_analisis
Yohan jacobi gaussseidel_analisis
 

More from Hitesh Kumar

Abstraction in c++ and Real Life Example of Abstraction in C++
Abstraction in c++ and Real Life Example of Abstraction in C++Abstraction in c++ and Real Life Example of Abstraction in C++
Abstraction in c++ and Real Life Example of Abstraction in C++Hitesh Kumar
 
HTML Interview Questions | Basic Html Interview Questions
HTML Interview Questions | Basic Html Interview QuestionsHTML Interview Questions | Basic Html Interview Questions
HTML Interview Questions | Basic Html Interview QuestionsHitesh Kumar
 
Fibonacci Series Program in C++
Fibonacci Series Program in C++Fibonacci Series Program in C++
Fibonacci Series Program in C++Hitesh Kumar
 
CSS Interview Questions for Fresher and Experience
CSS Interview Questions for Fresher and Experience CSS Interview Questions for Fresher and Experience
CSS Interview Questions for Fresher and Experience Hitesh Kumar
 
Factorial Program in C
Factorial Program in CFactorial Program in C
Factorial Program in CHitesh Kumar
 
Prime number program in C
Prime number program in CPrime number program in C
Prime number program in CHitesh Kumar
 
Top JSON Interview Questions for Freshers
Top JSON Interview Questions for FreshersTop JSON Interview Questions for Freshers
Top JSON Interview Questions for FreshersHitesh Kumar
 
Queue in C, Queue Real Life of Example
Queue in C, Queue Real Life of ExampleQueue in C, Queue Real Life of Example
Queue in C, Queue Real Life of ExampleHitesh Kumar
 
Fibonacci series c++
Fibonacci series c++Fibonacci series c++
Fibonacci series c++Hitesh Kumar
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in javaHitesh Kumar
 
Encapsulation in C++
Encapsulation in C++Encapsulation in C++
Encapsulation in C++Hitesh Kumar
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in javaHitesh Kumar
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in javaHitesh Kumar
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in javaHitesh Kumar
 
Constructor in java
Constructor in javaConstructor in java
Constructor in javaHitesh Kumar
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in javaHitesh Kumar
 
File handling in C++
File handling in C++File handling in C++
File handling in C++Hitesh Kumar
 
Main method in java
Main method in javaMain method in java
Main method in javaHitesh Kumar
 

More from Hitesh Kumar (20)

Abstraction in c++ and Real Life Example of Abstraction in C++
Abstraction in c++ and Real Life Example of Abstraction in C++Abstraction in c++ and Real Life Example of Abstraction in C++
Abstraction in c++ and Real Life Example of Abstraction in C++
 
HTML Interview Questions | Basic Html Interview Questions
HTML Interview Questions | Basic Html Interview QuestionsHTML Interview Questions | Basic Html Interview Questions
HTML Interview Questions | Basic Html Interview Questions
 
Fibonacci Series Program in C++
Fibonacci Series Program in C++Fibonacci Series Program in C++
Fibonacci Series Program in C++
 
CSS Interview Questions for Fresher and Experience
CSS Interview Questions for Fresher and Experience CSS Interview Questions for Fresher and Experience
CSS Interview Questions for Fresher and Experience
 
Factorial Program in C
Factorial Program in CFactorial Program in C
Factorial Program in C
 
Prime number program in C
Prime number program in CPrime number program in C
Prime number program in C
 
Top JSON Interview Questions for Freshers
Top JSON Interview Questions for FreshersTop JSON Interview Questions for Freshers
Top JSON Interview Questions for Freshers
 
Queue in C, Queue Real Life of Example
Queue in C, Queue Real Life of ExampleQueue in C, Queue Real Life of Example
Queue in C, Queue Real Life of Example
 
Fibonacci series c++
Fibonacci series c++Fibonacci series c++
Fibonacci series c++
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Encapsulation in C++
Encapsulation in C++Encapsulation in C++
Encapsulation in C++
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Ternary operator
Ternary operatorTernary operator
Ternary operator
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
 
Main method in java
Main method in javaMain method in java
Main method in java
 

Recently uploaded

30 ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...
30 ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...30 ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...
30 ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...Nguyen Thanh Tu Collection
 
TUYỂN TẬP 20 ĐỀ THI KHẢO SÁT HỌC SINH GIỎI MÔN TIẾNG ANH LỚP 6 NĂM 2020 (CÓ Đ...
TUYỂN TẬP 20 ĐỀ THI KHẢO SÁT HỌC SINH GIỎI MÔN TIẾNG ANH LỚP 6 NĂM 2020 (CÓ Đ...TUYỂN TẬP 20 ĐỀ THI KHẢO SÁT HỌC SINH GIỎI MÔN TIẾNG ANH LỚP 6 NĂM 2020 (CÓ Đ...
TUYỂN TẬP 20 ĐỀ THI KHẢO SÁT HỌC SINH GIỎI MÔN TIẾNG ANH LỚP 6 NĂM 2020 (CÓ Đ...Nguyen Thanh Tu Collection
 
أَسَانِيدُ كُتُبِ وَأُصُولِ النَّشْرِ لِابْنِ الْجَزَرِيِّ وَالْوَصْلُ بِهَا....
أَسَانِيدُ كُتُبِ وَأُصُولِ النَّشْرِ لِابْنِ الْجَزَرِيِّ وَالْوَصْلُ بِهَا....أَسَانِيدُ كُتُبِ وَأُصُولِ النَّشْرِ لِابْنِ الْجَزَرِيِّ وَالْوَصْلُ بِهَا....
أَسَانِيدُ كُتُبِ وَأُصُولِ النَّشْرِ لِابْنِ الْجَزَرِيِّ وَالْوَصْلُ بِهَا....سمير بسيوني
 
TUYỂN TẬP 25 ĐỀ THI HỌC SINH GIỎI MÔN TIẾNG ANH LỚP 6 NĂM 2023 CÓ ĐÁP ÁN (SƯU...
TUYỂN TẬP 25 ĐỀ THI HỌC SINH GIỎI MÔN TIẾNG ANH LỚP 6 NĂM 2023 CÓ ĐÁP ÁN (SƯU...TUYỂN TẬP 25 ĐỀ THI HỌC SINH GIỎI MÔN TIẾNG ANH LỚP 6 NĂM 2023 CÓ ĐÁP ÁN (SƯU...
TUYỂN TẬP 25 ĐỀ THI HỌC SINH GIỎI MÔN TIẾNG ANH LỚP 6 NĂM 2023 CÓ ĐÁP ÁN (SƯU...Nguyen Thanh Tu Collection
 
French Revolution (फ्रेंच राज्यक्रांती)
French Revolution  (फ्रेंच राज्यक्रांती)French Revolution  (फ्रेंच राज्यक्रांती)
French Revolution (फ्रेंच राज्यक्रांती)Shankar Aware
 
، ژیانا ئینگلیزا ب کوردی ، ئینگلیزەکان ، راپورتی کوردی ، راپورتا مێژوی ، ژ...
، ژیانا ئینگلیزا ب کوردی ، ئینگلیزەکان ، راپورتی کوردی ،    راپورتا مێژوی ، ژ...، ژیانا ئینگلیزا ب کوردی ، ئینگلیزەکان ، راپورتی کوردی ،    راپورتا مێژوی ، ژ...
، ژیانا ئینگلیزا ب کوردی ، ئینگلیزەکان ، راپورتی کوردی ، راپورتا مێژوی ، ژ...Idrees.Hishyar
 

Recently uploaded (6)

30 ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...
30 ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...30 ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...
30 ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...
 
TUYỂN TẬP 20 ĐỀ THI KHẢO SÁT HỌC SINH GIỎI MÔN TIẾNG ANH LỚP 6 NĂM 2020 (CÓ Đ...
TUYỂN TẬP 20 ĐỀ THI KHẢO SÁT HỌC SINH GIỎI MÔN TIẾNG ANH LỚP 6 NĂM 2020 (CÓ Đ...TUYỂN TẬP 20 ĐỀ THI KHẢO SÁT HỌC SINH GIỎI MÔN TIẾNG ANH LỚP 6 NĂM 2020 (CÓ Đ...
TUYỂN TẬP 20 ĐỀ THI KHẢO SÁT HỌC SINH GIỎI MÔN TIẾNG ANH LỚP 6 NĂM 2020 (CÓ Đ...
 
أَسَانِيدُ كُتُبِ وَأُصُولِ النَّشْرِ لِابْنِ الْجَزَرِيِّ وَالْوَصْلُ بِهَا....
أَسَانِيدُ كُتُبِ وَأُصُولِ النَّشْرِ لِابْنِ الْجَزَرِيِّ وَالْوَصْلُ بِهَا....أَسَانِيدُ كُتُبِ وَأُصُولِ النَّشْرِ لِابْنِ الْجَزَرِيِّ وَالْوَصْلُ بِهَا....
أَسَانِيدُ كُتُبِ وَأُصُولِ النَّشْرِ لِابْنِ الْجَزَرِيِّ وَالْوَصْلُ بِهَا....
 
TUYỂN TẬP 25 ĐỀ THI HỌC SINH GIỎI MÔN TIẾNG ANH LỚP 6 NĂM 2023 CÓ ĐÁP ÁN (SƯU...
TUYỂN TẬP 25 ĐỀ THI HỌC SINH GIỎI MÔN TIẾNG ANH LỚP 6 NĂM 2023 CÓ ĐÁP ÁN (SƯU...TUYỂN TẬP 25 ĐỀ THI HỌC SINH GIỎI MÔN TIẾNG ANH LỚP 6 NĂM 2023 CÓ ĐÁP ÁN (SƯU...
TUYỂN TẬP 25 ĐỀ THI HỌC SINH GIỎI MÔN TIẾNG ANH LỚP 6 NĂM 2023 CÓ ĐÁP ÁN (SƯU...
 
French Revolution (फ्रेंच राज्यक्रांती)
French Revolution  (फ्रेंच राज्यक्रांती)French Revolution  (फ्रेंच राज्यक्रांती)
French Revolution (फ्रेंच राज्यक्रांती)
 
، ژیانا ئینگلیزا ب کوردی ، ئینگلیزەکان ، راپورتی کوردی ، راپورتا مێژوی ، ژ...
، ژیانا ئینگلیزا ب کوردی ، ئینگلیزەکان ، راپورتی کوردی ،    راپورتا مێژوی ، ژ...، ژیانا ئینگلیزا ب کوردی ، ئینگلیزەکان ، راپورتی کوردی ،    راپورتا مێژوی ، ژ...
، ژیانا ئینگلیزا ب کوردی ، ئینگلیزەکان ، راپورتی کوردی ، راپورتا مێژوی ، ژ...
 

Quick sort

  • 1. /*** program of quick sort **** * http://www.tutorial4us.com/data-structure/c-quick-sort */ #include<stdio.h> #include<conio.h> void quicksort(int [],int,int); int partition(int [],int,int); main() { int a[20],p,q,i,n; clrscr(); printf("How many elements you want to enter: "); scanf("%d",&n); for(i=0; i<n; i++) { printf("nEnter any %d elements: "n); scanf("%d",&a[i]); } p=0; q=n-1;
  • 2. printf("nnnArray Befor Sorting : "); for(i=0; i<n; i++) { delay(500); printf("%5d",a[i]); } quicksort(a,p,q); printf("nnnArray After Sorting : "); for(i=0; i<n; i++) { delay(500); printf("%5d",a[i]); } getch(); return 0; } void quicksort(int a[],int p,int q) { int j; if(p<q) { j=partition(a,p,q+1);
  • 3. quicksort(a,p,j-1); quicksort(a,j+1,q); } } int partition(int a[],int m,int p) { int v,i,j; int temp; v=a[m]; i=m;j=p; do { do { i += 1; } while(a[i]<v); do { j -= 1; } while(a[j]>v); if(i<j)
  • 4. { temp = a[i]; a[i] = a[j]; a[j] = temp; } } while(i<j); a[m] =a[j]; a[j] = v; return j; }