Your SlideShare is downloading. ×
week-21x
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

week-21x

185
views

Published on

Published in: Education

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
185
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 methods to sort a given list of integers inascending order:ii) Merge sort */#include <stdio.h>#include <stdlib.h>#define MAX_ARY 10void merge_sort(int x[], int end, int start);int main(void) {int ary[MAX_ARY];int j = 0;printf("nnEnter the elements to be sorted: n"); for(j=0;j<MAX_ARY;j++) scanf("%d",&ary[j]);/* array before mergesort */printf("Before :");for(j = 0; j < MAX_ARY; j++) printf(" %d", ary[j]);printf("n");
  • 2. merge_sort(ary, 0, MAX_ARY - 1);/* array after mergesort */printf("After Merge Sort :");for(j = 0; j < MAX_ARY; j++) printf(" %d", ary[j]);printf("n");getch();}/* Method to implement Merge Sort*/void merge_sort(int x[], int end, int start) {int j = 0;const int size = start - end + 1;int mid = 0;int mrg1 = 0;int mrg2 = 0;int executing[MAX_ARY];if(end == start) return;
  • 3. mid = (end + start) / 2;merge_sort(x, end, mid);merge_sort(x, mid + 1, start);for(j = 0; j < size; j++) executing[j] = x[end + j];mrg1 = 0;mrg2 = mid - end + 1;for(j = 0; j < size; j++) { if(mrg2 <= start - end) if(mrg1 <= mid - end) if(executing[mrg1] > executing[mrg2]) x[j + end] = executing[mrg2++]; else x[j + end] = executing[mrg1++]; else x[j + end] = executing[mrg2++]; else x[j + end] = executing[mrg1++];}}
  • 4. /* Write C program that implement the following sorting methodsto sort a given list of integers in ascending order: i) Insertion sort */#include<stdio.h>#include<conio.h>void inst_sort(int []);void main(){ int num[5],count; clrscr(); printf("nEnter the Five Elements to sort:n"); for (count=0;count<5;count++) scanf("%d",&num[count]); inst_sort(num); printf("nnElements after sorting: n"); for(count=0;count<5;count++) printf("%dn",num[count]);
  • 5. getch();}// Function for Insertion Sortingvoid inst_sort(int num[]){int i,j,k;for(j=1;j<5;j++) { k=num[j]; for(i=j-1;i>=0 && k<num[i];i--) num[i+1]=num[i]; num[i+1]=k; }}