SlideShare a Scribd company logo
1 of 6
/* Write C program that implement the following sorting methods

to sort a given list of integers in ascending order: ii) Quick sort */



#include <stdio.h>

#define MAX 10



void swap(int *m,int *n)

{

    int temp;

    temp = *m;

    *m = *n;

    *n = temp;

}

int get_key_position(int x,int y )

{

    return((x+y) /2);

}



// Function for Quick Sort

void quicksort(int list[],int m,int n)

{

    int key,i,j,k;

    if( m < n)

    {
k = get_key_position(m,n);

        swap(&list[m],&list[k]);

        key = list[m];

        i = m+1;

        j = n;

        while(i <= j)

        {

            while((i <= n) && (list[i] <= key))

                 i++;

            while((j >= m) && (list[j] > key))

                            j--;

               if( i < j)

                            swap(&list[i],&list[j]);

        }

        swap(&list[m],&list[j]);

        quicksort(list,m,j-1);

        quicksort(list,j+1,n);

    }

}



// Function to read the data

void read_data(int list[],int n)

{

    int j;
printf("nnEnter the elements:n");

    for(j=0;j<n;j++)

       scanf("%d",&list[j]);

}



// Function to print the data

void print_data(int list[],int n)

{

    int j;

    for(j=0;j<n;j++)

       printf("%dt",list[j]);

}



void main()

{

    int list[MAX], num;

    clrscr();

    printf("n***** Enter the number of elements Maximum [10] *****n");

    scanf("%d",&num);

    read_data(list,num);

    printf("nnElements in the list before sorting are:n");

    print_data(list,num);

    quicksort(list,0,num-1);

    printf("nnElements in the list after sorting are:n");
print_data(list,num);

    getch();

}

/* Write C programs that implement the following sorting methods to sort

    a given list of integers in ascending order: i) Bubble sort */



#include <stdio.h>

#define MAX 10



void swapList(int *m,int *n)

{

    int temp;

    temp = *m;

    *m = *n;

    *n = temp;

}



// Function for Bubble Sort

void bub_sort(int list[], int n)

{

    int i,j;

    for(i=0;i<(n-1);i++)

      for(j=0;j<(n-(i+1));j++)

                if(list[j] > list[j+1])
swapList(&list[j],&list[j+1]);

}



void readlist(int list[],int n)

{

    int j;

    printf("nEnter the elements: n");

    for(j=0;j<n;j++)

       scanf("%d",&list[j]);

}



// Showing the contents of the list

void printlist(int list[],int n)

{

    int j;

    for(j=0;j<n;j++)

      printf("%dt",list[j]);

}



void main()

{

    int list[MAX], num;

    clrscr();

    printf("nnn***** Enter the number of elements [Maximum 10] *****n");
scanf("%d",&num);

    readlist(list,num);

    printf("nnElements in the list before sorting are:n");

    printlist(list,num);

    bub_sort(list,num);

    printf("nnElements in the list after sorting are:n");

    printlist(list,num);

    getch();

}

More Related Content

What's hot (20)

Final ds record
Final ds recordFinal ds record
Final ds record
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
 
C program to implement linked list using array abstract data type
C program to implement linked list using array abstract data typeC program to implement linked list using array abstract data type
C program to implement linked list using array abstract data type
 
Avl tree
Avl treeAvl tree
Avl tree
 
Composition in JavaScript
Composition in JavaScriptComposition in JavaScript
Composition in JavaScript
 
week-16x
week-16xweek-16x
week-16x
 
Linked list searching deleting inserting
Linked list searching deleting insertingLinked list searching deleting inserting
Linked list searching deleting inserting
 
Linked list imp of list
Linked list imp of listLinked list imp of list
Linked list imp of list
 
Circular queue
Circular queueCircular queue
Circular queue
 
Recursion concepts by Divya
Recursion concepts by DivyaRecursion concepts by Divya
Recursion concepts by Divya
 
Stack concepts by Divya
Stack concepts by DivyaStack concepts by Divya
Stack concepts by Divya
 
Brief intro to clojure
Brief intro to clojureBrief intro to clojure
Brief intro to clojure
 
Jarmo van de Seijp Shadbox ERC223
Jarmo van de Seijp Shadbox ERC223Jarmo van de Seijp Shadbox ERC223
Jarmo van de Seijp Shadbox ERC223
 
cosc 281 hw2
cosc 281 hw2cosc 281 hw2
cosc 281 hw2
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in c
 
Class array
Class arrayClass array
Class array
 
Short intro to the Rust language
Short intro to the Rust languageShort intro to the Rust language
Short intro to the Rust language
 
Oopsprc1c
Oopsprc1cOopsprc1c
Oopsprc1c
 
Vcs29
Vcs29Vcs29
Vcs29
 
week-6x
week-6xweek-6x
week-6x
 

Viewers also liked

All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkarsandeep kumbhkar
 
C programming language
C programming languageC programming language
C programming languageAbin Rimal
 
Palindrome number program in c
Palindrome number program in cPalindrome number program in c
Palindrome number program in cmohdshanu
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in CSyed Mustafa
 
Module 03 File Handling in C
Module 03 File Handling in CModule 03 File Handling in C
Module 03 File Handling in CTushar B Kute
 
File handling in c
File handling in c File handling in c
File handling in c Vikash Dhal
 
UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CAshim Lamichhane
 
C programs
C programsC programs
C programsMinu S
 
File handling in c
File handling in cFile handling in c
File handling in caakanksha s
 
Introduction to CSS
Introduction to CSSIntroduction to CSS
Introduction to CSSAmit Tyagi
 

Viewers also liked (18)

All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
C programming language
C programming languageC programming language
C programming language
 
Palindrome number program in c
Palindrome number program in cPalindrome number program in c
Palindrome number program in c
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
 
Module 03 File Handling in C
Module 03 File Handling in CModule 03 File Handling in C
Module 03 File Handling in C
 
C lab-programs
C lab-programsC lab-programs
C lab-programs
 
Structure c
Structure cStructure c
Structure c
 
File in c
File in cFile in c
File in c
 
File handling in c
File handling in c File handling in c
File handling in c
 
UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in C
 
C programs
C programsC programs
C programs
 
C Programming
C ProgrammingC Programming
C Programming
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Structure in c
Structure in cStructure in c
Structure in c
 
Introduction to CSS
Introduction to CSSIntroduction to CSS
Introduction to CSS
 
Array in C
Array in CArray in C
Array in C
 
Function in C program
Function in C programFunction in C program
Function in C program
 
CSS ppt
CSS pptCSS ppt
CSS ppt
 

Similar to week-20x

Similar to week-20x (20)

Sorting programs
Sorting programsSorting programs
Sorting programs
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
C programs
C programsC programs
C programs
 
DAA Lab Work.docx
DAA Lab Work.docxDAA Lab Work.docx
DAA Lab Work.docx
 
Pnno
PnnoPnno
Pnno
 
week-19x
week-19xweek-19x
week-19x
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C Programs
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
unit-2-dsa.pptx
unit-2-dsa.pptxunit-2-dsa.pptx
unit-2-dsa.pptx
 
week-18x
week-18xweek-18x
week-18x
 
Implement the ListArray ADT-Implement the following operations.pdf
Implement the ListArray ADT-Implement the following operations.pdfImplement the ListArray ADT-Implement the following operations.pdf
Implement the ListArray ADT-Implement the following operations.pdf
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory Mapping
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
PRACTICAL COMPUTING
PRACTICAL COMPUTINGPRACTICAL COMPUTING
PRACTICAL COMPUTING
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
programs
programsprograms
programs
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
Questions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdfQuestions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdf
 
An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL
 
Tugas1
Tugas1Tugas1
Tugas1
 

More from KITE www.kitecolleges.com (20)

DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENTDISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
 
BrainFingerprintingpresentation
BrainFingerprintingpresentationBrainFingerprintingpresentation
BrainFingerprintingpresentation
 
ch6
ch6ch6
ch6
 
week-11x
week-11xweek-11x
week-11x
 
PPT (2)
PPT (2)PPT (2)
PPT (2)
 
week-10x
week-10xweek-10x
week-10x
 
week-1x
week-1xweek-1x
week-1x
 
ch14
ch14ch14
ch14
 
ch16
ch16ch16
ch16
 
holographic versatile disc
holographic versatile discholographic versatile disc
holographic versatile disc
 
week-22x
week-22xweek-22x
week-22x
 
week-3x
week-3xweek-3x
week-3x
 
ch8
ch8ch8
ch8
 
Intro Expert Systems test-me.co.uk
Intro Expert Systems test-me.co.ukIntro Expert Systems test-me.co.uk
Intro Expert Systems test-me.co.uk
 
ch17
ch17ch17
ch17
 
ch4
ch4ch4
ch4
 
week-7x
week-7xweek-7x
week-7x
 
week-9x
week-9xweek-9x
week-9x
 
week-4x
week-4xweek-4x
week-4x
 
week-14x
week-14xweek-14x
week-14x
 

Recently uploaded

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 

Recently uploaded (20)

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 

week-20x

  • 1. /* Write C program that implement the following sorting methods to sort a given list of integers in ascending order: ii) Quick sort */ #include <stdio.h> #define MAX 10 void swap(int *m,int *n) { int temp; temp = *m; *m = *n; *n = temp; } int get_key_position(int x,int y ) { return((x+y) /2); } // Function for Quick Sort void quicksort(int list[],int m,int n) { int key,i,j,k; if( m < n) {
  • 2. k = get_key_position(m,n); swap(&list[m],&list[k]); key = list[m]; i = m+1; j = n; while(i <= j) { while((i <= n) && (list[i] <= key)) i++; while((j >= m) && (list[j] > key)) j--; if( i < j) swap(&list[i],&list[j]); } swap(&list[m],&list[j]); quicksort(list,m,j-1); quicksort(list,j+1,n); } } // Function to read the data void read_data(int list[],int n) { int j;
  • 3. printf("nnEnter the elements:n"); for(j=0;j<n;j++) scanf("%d",&list[j]); } // Function to print the data void print_data(int list[],int n) { int j; for(j=0;j<n;j++) printf("%dt",list[j]); } void main() { int list[MAX], num; clrscr(); printf("n***** Enter the number of elements Maximum [10] *****n"); scanf("%d",&num); read_data(list,num); printf("nnElements in the list before sorting are:n"); print_data(list,num); quicksort(list,0,num-1); printf("nnElements in the list after sorting are:n");
  • 4. print_data(list,num); getch(); } /* Write C programs that implement the following sorting methods to sort a given list of integers in ascending order: i) Bubble sort */ #include <stdio.h> #define MAX 10 void swapList(int *m,int *n) { int temp; temp = *m; *m = *n; *n = temp; } // Function for Bubble Sort void bub_sort(int list[], int n) { int i,j; for(i=0;i<(n-1);i++) for(j=0;j<(n-(i+1));j++) if(list[j] > list[j+1])
  • 5. swapList(&list[j],&list[j+1]); } void readlist(int list[],int n) { int j; printf("nEnter the elements: n"); for(j=0;j<n;j++) scanf("%d",&list[j]); } // Showing the contents of the list void printlist(int list[],int n) { int j; for(j=0;j<n;j++) printf("%dt",list[j]); } void main() { int list[MAX], num; clrscr(); printf("nnn***** Enter the number of elements [Maximum 10] *****n");
  • 6. scanf("%d",&num); readlist(list,num); printf("nnElements in the list before sorting are:n"); printlist(list,num); bub_sort(list,num); printf("nnElements in the list after sorting are:n"); printlist(list,num); getch(); }