SlideShare a Scribd company logo
/* 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

ODP
Functors, applicatives, monads
DOCX
Program: Inheritance in Class - to find topper out of 10 students
DOCX
Concatenation of two strings using class in c++
PDF
Program to sort the n names in an alphabetical order
Functors, applicatives, monads
Program: Inheritance in Class - to find topper out of 10 students
Concatenation of two strings using class in c++
Program to sort the n names in an alphabetical order

What's hot (20)

DOC
Final ds record
PPT
Doublylinklist
DOCX
C program to implement linked list using array abstract data type
PDF
Composition in JavaScript
PDF
Linked list searching deleting inserting
DOCX
Linked list imp of list
DOCX
Circular queue
PDF
Recursion concepts by Divya
PDF
Stack concepts by Divya
PDF
Brief intro to clojure
DOCX
Jarmo van de Seijp Shadbox ERC223
DOCX
cosc 281 hw2
DOCX
Mouse programming in c
DOCX
Class array
PDF
Short intro to the Rust language
PDF
Oopsprc1c
Final ds record
Doublylinklist
C program to implement linked list using array abstract data type
Composition in JavaScript
Linked list searching deleting inserting
Linked list imp of list
Circular queue
Recursion concepts by Divya
Stack concepts by Divya
Brief intro to clojure
Jarmo van de Seijp Shadbox ERC223
cosc 281 hw2
Mouse programming in c
Class array
Short intro to the Rust language
Oopsprc1c
Ad

Viewers also liked (18)

PPT
All important c programby makhan kumbhkar
PPTX
C programming language
DOC
Palindrome number program in c
PDF
Pointers and call by value, reference, address in C
PDF
Module 03 File Handling in C
DOC
C lab-programs
PPT
Structure c
PPT
File in c
PPT
File handling in c
PPTX
UNIT 10. Files and file handling in C
DOCX
C programs
DOCX
C Programming
PPTX
File handling in c
PPT
Structure in c
PPT
Introduction to CSS
PPTX
Array in C
PPTX
Function in C program
PPT
All important c programby makhan kumbhkar
C programming language
Palindrome number program in c
Pointers and call by value, reference, address in C
Module 03 File Handling in C
C lab-programs
Structure c
File in c
File handling in c
UNIT 10. Files and file handling in C
C programs
C Programming
File handling in c
Structure in c
Introduction to CSS
Array in C
Function in C program
Ad

Similar to week-20x (20)

DOC
Sorting programs
PPTX
C Programming Language Part 8
PDF
C programs
DOCX
DAA Lab Work.docx
DOCX
DAA Lab File C Programs
DOCX
Data Structures Using C Practical File
PPTX
unit-2-dsa.pptx
PDF
codes.txt.pdf code presentation engineering
PDF
Implement the ListArray ADT-Implement the following operations.pdf
DOCX
cs class 12 project computer science .docx
PPT
02 Arrays And Memory Mapping
PPTX
L25-L26-Parameter passing techniques.pptx
DOCX
PRACTICAL COMPUTING
DOCX
PDF
programs
DOC
Daapracticals 111105084852-phpapp02
PDF
Questions has 4 parts.1st part Program to implement sorting algor.pdf
Sorting programs
C Programming Language Part 8
C programs
DAA Lab Work.docx
DAA Lab File C Programs
Data Structures Using C Practical File
unit-2-dsa.pptx
codes.txt.pdf code presentation engineering
Implement the ListArray ADT-Implement the following operations.pdf
cs class 12 project computer science .docx
02 Arrays And Memory Mapping
L25-L26-Parameter passing techniques.pptx
PRACTICAL COMPUTING
programs
Daapracticals 111105084852-phpapp02
Questions has 4 parts.1st part Program to implement sorting algor.pdf

More from KITE www.kitecolleges.com (20)

Recently uploaded (20)

PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Cell Types and Its function , kingdom of life
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
SOIL: Factor, Horizon, Process, Classification, Degradation, Conservation
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Indian roads congress 037 - 2012 Flexible pavement
PPTX
History, Philosophy and sociology of education (1).pptx
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Cell Types and Its function , kingdom of life
Final Presentation General Medicine 03-08-2024.pptx
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Weekly quiz Compilation Jan -July 25.pdf
Computing-Curriculum for Schools in Ghana
SOIL: Factor, Horizon, Process, Classification, Degradation, Conservation
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Practical Manual AGRO-233 Principles and Practices of Natural Farming
Supply Chain Operations Speaking Notes -ICLT Program
A systematic review of self-coping strategies used by university students to ...
Paper A Mock Exam 9_ Attempt review.pdf.
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
LDMMIA Reiki Yoga Finals Review Spring Summer
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Indian roads congress 037 - 2012 Flexible pavement
History, Philosophy and sociology of education (1).pptx
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
A powerpoint presentation on the Revised K-10 Science Shaping Paper

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