Your SlideShare is downloading. ×
week-20x
week-20x
week-20x
week-20x
week-20x
week-20x
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
166
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. /* Write C program that implement the following sorting methodsto sort a given list of integers in ascending order: ii) Quick sort */#include <stdio.h>#define MAX 10void 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 Sortvoid 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 datavoid 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 datavoid 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 10void swapList(int *m,int *n){ int temp; temp = *m; *m = *n; *n = temp;}// Function for Bubble Sortvoid 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 listvoid 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();}

×