SlideShare una empresa de Scribd logo
1 de 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();

}

Más contenido relacionado

La actualidad más candente (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
 

Destacado

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
 

Destacado (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 a week-20x

Similar a 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
 

Más de 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
 

Último

Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
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
 
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
 
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
 
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
 
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
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
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
 
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
 

Último (20)

Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
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
 
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
 
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
 
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
 
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
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
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...
 
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
 

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(); }