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) Merge sort */



#include <stdio.h>

#include <stdlib.h>



#define MAX_ARY 10



void 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");
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;
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++];

}

}
/* Write C program that implement the following sorting methods

to 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]);
getch();

}



// Function for Insertion Sorting

void 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;

    }

}

More Related Content

PDF
Insertion sort
PDF
Program in ‘C’ language to implement linear search using pointers
PDF
Bcsl 033 data and file structures lab s5-2
PDF
Bcsl 033 data and file structures lab s5-3
PDF
Implement a queue using two stacks.
PDF
Bcsl 033 data and file structures lab s2-2
Insertion sort
Program in ‘C’ language to implement linear search using pointers
Bcsl 033 data and file structures lab s5-2
Bcsl 033 data and file structures lab s5-3
Implement a queue using two stacks.
Bcsl 033 data and file structures lab s2-2

What's hot (20)

PPSX
C programming array & shorting
DOC
Final ds record
DOCX
One dimensional operation of Array in C- language
DOCX
C program to implement linked list using array abstract data type
PDF
Bcsl 033 data and file structures lab s3-3
PDF
Linear search
PDF
SATySFiのこれからの課題たち
DOCX
Document
PPTX
Examples sandhiya class'
PPTX
C & Python Introduction
PDF
C programs Set 2
PPTX
Double linked list
PPTX
Computer programing w
DOCX
New microsoft word document
DOCX
cosc 281 hw2
C programming array & shorting
Final ds record
One dimensional operation of Array in C- language
C program to implement linked list using array abstract data type
Bcsl 033 data and file structures lab s3-3
Linear search
SATySFiのこれからの課題たち
Document
Examples sandhiya class'
C & Python Introduction
C programs Set 2
Double linked list
Computer programing w
New microsoft word document
cosc 281 hw2
Ad

Viewers also liked (18)

PDF
C programming part1
PPT
PPT
腾讯大讲堂28 用户与产品的桥梁
PDF
2011 03 auto.sk
PDF
Gemius
PDF
Com scorewhithertheclickeurope0309
PDF
基于Eucalyptus的教育知识服务体系模型研究
ODP
Hadoop @ Sara & BiG Grid
ODP
использование свободного программного обеспечения в ВУЗах Украины
PPT
腾讯大讲堂36 竞争情报入门
PPT
C++ basics
DOCX
C interview question answer 2
ODP
Open Source Бизнес или о том как открыть 90% и остаться со штанами
PPT
BrainFingerprintingpresentation
PPT
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
PDF
Deep C Programming
PPTX
C programming interview questions
C programming part1
腾讯大讲堂28 用户与产品的桥梁
2011 03 auto.sk
Gemius
Com scorewhithertheclickeurope0309
基于Eucalyptus的教育知识服务体系模型研究
Hadoop @ Sara & BiG Grid
использование свободного программного обеспечения в ВУЗах Украины
腾讯大讲堂36 竞争情报入门
C++ basics
C interview question answer 2
Open Source Бизнес или о том как открыть 90% и остаться со штанами
BrainFingerprintingpresentation
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
Deep C Programming
C programming interview questions
Ad

Similar to week-21x (20)

DOCX
Write a complete C programme to implement the following sorting algor.docx
DOCX
DAA Lab Work.docx
DOCX
DAA Lab File C Programs
DOCX
ADA FILE
DOC
Sorting programs
PDF
Sorting Algorithms and their implementations
DOCX
DOCX
Write a program to check a given number is prime or not
DOCX
PPS 7.7 RECURSION, AS A DIFFERENT WAY OF SOLVING PROBLEMS. EXAMPLE PROGRAMS
PPTX
Merge sort
PPTX
PPT.pptx Searching and Sorting Techniques
DOCX
Write a C program that implements a simple array-based insertion sort-.docx
PPTX
LectureSlidData_sturcture_algorithm_v2.pptx
DOC
Ada file
DOCX
Merge sort in CSolution#include-stdio-h- #define MAX 50 void mergeSort.docx
PPTX
sorting1.pptx
DOC
Basic c programs updated on 31.8.2020
PPT
Insert Sort & Merge Sort Using C Programming
Write a complete C programme to implement the following sorting algor.docx
DAA Lab Work.docx
DAA Lab File C Programs
ADA FILE
Sorting programs
Sorting Algorithms and their implementations
Write a program to check a given number is prime or not
PPS 7.7 RECURSION, AS A DIFFERENT WAY OF SOLVING PROBLEMS. EXAMPLE PROGRAMS
Merge sort
PPT.pptx Searching and Sorting Techniques
Write a C program that implements a simple array-based insertion sort-.docx
LectureSlidData_sturcture_algorithm_v2.pptx
Ada file
Merge sort in CSolution#include-stdio-h- #define MAX 50 void mergeSort.docx
sorting1.pptx
Basic c programs updated on 31.8.2020
Insert Sort & Merge Sort Using C Programming

More from KITE www.kitecolleges.com (20)

Recently uploaded (20)

PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Pharma ospi slides which help in ospi learning
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Cell Types and Its function , kingdom of life
PPTX
Lesson notes of climatology university.
PDF
Pre independence Education in Inndia.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Institutional Correction lecture only . . .
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
TR - Agricultural Crops Production NC III.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Anesthesia in Laparoscopic Surgery in India
Pharma ospi slides which help in ospi learning
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Renaissance Architecture: A Journey from Faith to Humanism
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Cell Types and Its function , kingdom of life
Lesson notes of climatology university.
Pre independence Education in Inndia.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
01-Introduction-to-Information-Management.pdf
Institutional Correction lecture only . . .
Abdominal Access Techniques with Prof. Dr. R K Mishra
O5-L3 Freight Transport Ops (International) V1.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Supply Chain Operations Speaking Notes -ICLT Program
TR - Agricultural Crops Production NC III.pdf

week-21x

  • 1. /* Write C program that implement the following sorting methods to sort a given list of integers in ascending order: ii) Merge sort */ #include <stdio.h> #include <stdlib.h> #define MAX_ARY 10 void 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 methods to 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 Sorting void 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; } }