Вы находитесь на странице: 1из 10

2012

Sorting in Data Structure


Top 6 Sorting in Data Structure Programming
This Mini Book contains only codes of different types of sorting in Data Structure

Amrit Razz Shrestha


2/25/2012

Name: Amrit Razz shrestha Dob: December 15th 1991 Qualification: Bachelor in information management [currently in 3rd

semester]
Email: raj_axle36@yahoo.com amrit.shrestha12@gmail.com amrit_razz@live.com (Please send me ur feedback about this book so that I will be encourage to post other such mini book for you in the future) -Author

2|Page

Bubble sort
#include <conio.h> #include <stdio.h> void bubblesort (int a[] , int n) { int temp,i,j; for (i=0;i<n;i++) { for (j=0;j<(n-i-1);j++) { if (a[j] > a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } } void main() { int a[50],i,n; clrscr(); printf ("Enter the number of array::"); scanf("%d",&n); for (i=0;i<n;i++) { printf ("Enter %d item::",i+1); scanf ("%d",&a[i]); } bubblesort (a,n); printf ("The sorted array is::\n"); for (i=0;i<n;i++) printf ("%d\t",a[i]); getch(); }

3|Page

Insertion sort
#include<stdio.h> #include<conio.h> void main() { int a[100],n,i,j,k,temp; clrscr(); printf ("Enter the numbers of term::"); scanf("%d",&n); for (i=0;i<n;i++) { printf("Enter %d item::",i+1); scanf ("%d",&a[i]); } for (k=1;k<=n-1;k++) { temp = a[k]; j = k-1; while((temp < a[j]) && (j>=0)) { a[j+1] = a[j]; j=j-1; } a[j+1]=temp; } printf ("The sorted array is::\n"); for (i=0;i<n;i++) printf ("%d\t",a[i]); getch(); }

4|Page

Selection sort
#include <conio.h> #include <stdio.h> void selectionsort(int a[], int n) { int min,loc,temp,i,j; min = a[0]; for (i=0;i<n;i++) { min = a[i]; loc = i; for (j=i+1;j<n;j++) { if (a[j]<min) { min=a[j]; loc =j; } } if (loc != i) { temp = a[i]; a[i] = a[loc]; a[loc] = temp; }}} void main() { int a[100],n,i,j,k,temp; clrscr(); printf ("Enter the numbers of term::"); scanf("%d",&n); for (i=0;i<n;i++) { printf("Enter %d item::",i+1); scanf ("%d",&a[i]); }selectionsort(a,n); printf("\n\nAfter the sorting the array is\n"); for (i=0;i<n;i++) { printf("a[%d] = %d\n",i,a[i]); getch(); }
5|Page

Heap sort
//heap sort #include<stdio.h> #include<conio.h> int j,a[15],i,temp,n; void main() { clrscr(); void createheap(int); printf("How many numbers of elements\n(Not more than 15)"); scanf("%d",&n); printf("\nEnter the Elements:"); for(i=1;i<=n;i++) scanf("%d",&a[i]); for(i=n;i>=2;i--) createheap(i); for(i=n-1;i>=1;i--) { temp=a[1]; a[1]=a[i+1]; a[i+1]=temp; for(j=i;j>=2;j--) createheap(j); } printf("\nThe sorted elements are :\n"); for(i=1;i<=n;i++) printf("\n%d",a[i]); getch(); } void createheap(int y) { if(y>1) {if(a[y]>a[y/2]) {temp=a[y]; a[y]=a[y/2]; a[y/2]=temp; createheap(y/2); }}}
6|Page

Merge sort
// Merge sort #include<stdio.h> #include<conio.h> void mergesort(int num[],int temp[],int n); void m_sort(int num[],int temp[],int left,int right); void merge(int num[],int temp[],int left, int mid,int right); int num[100],temp[100],i; void main() { int n; clrscr(); printf("Enter size of array:"); scanf("%d",&n); printf("Enter The element :"); for(i=0;i<n;i++) { scanf("%d",&num[i]); } mergesort(num,temp,n); printf("Done with sort :"); for(i=0;i<n;i++) printf("\n%d\n",num[i]); getch(); } void mergesort(int num[],int temp[], int n) { m_sort(num,temp,0,n-1); } void m_sort(int num[],int temp[], int left, int right) { int mid; if(right>left) { mid=(right+left)/2; m_sort(num,temp,left,mid); m_sort(num,temp,mid+1,right); merge(num,temp,left,mid+1,right); } }

7|Page

void merge(int num[],int temp[],int left, int mid,int right) { int i,left_end,num_element, temp_pos; left_end=mid-1; temp_pos=left; num_element=left_end+1; while((left<=left_end)&&(mid<=right)) { if(num[left]<=num[mid]) { temp[temp_pos]=num[left]; temp_pos=temp_pos+1; left=left+1; } else { temp[temp_pos]=num[mid]; temp_pos=temp_pos+1; mid=mid+1; } } while(left<=left_end) { temp[temp_pos]=num[left]; left=left+1; temp_pos=temp_pos+1; } while(mid<=right) { temp[temp_pos]=num[mid]; mid=mid+1; temp_pos=temp_pos+1; } for(i=0;i<num_element;i++) { num[right]=temp[right]; right=right-1; } }

8|Page

Quick sort
#include<stdio.h> #include<conio.h> #define max 100 void output(int a[],int n); void quick_sort(int b[],int l,int h); void input (); int a[max],n,i,l,h; void main() { clrscr(); input(); getch(); } //input definition void input(void) { printf("How many elements in the array : "); scanf("%d",&n); printf("\n"); for(i=0;i<=n-1;i++) { printf("Enter the %d elemennts :", i+1); scanf("%d",&a[i]); } l=0; h=n-1; quick_sort(a,l,h); printf("\n\nThe Sorted Array :\n "); output(a,n); } //quicksort definition void quick_sort(int b[],int l, int h) { int temp,key,low,high; low=l;
9|Page

high=h; key=b[(low+high)/2]; do { while(key>b[low]) { low++; } while(key<b[high]) { high--; } if(low<=high) { temp=b[low]; b[low++]=b[high]; b[high--]=temp; } } while(low<=high); if(l<high) quick_sort(b,l,high); if(low<h) quick_sort(a,low,h); } //output definition void output(int a[],int n) { for(i=0;i<=n-1;i++) printf("\n%d",a[i]); }

10 | P a g e

Вам также может понравиться