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

What's hot

What's hot (20)

C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
 
week-11x
week-11xweek-11x
week-11x
 
Final ds record
Final ds recordFinal ds record
Final ds record
 
week-10x
week-10xweek-10x
week-10x
 
One dimensional operation of Array in C- language
One dimensional operation of Array in C- language One dimensional operation of Array in C- language
One dimensional operation of Array in C- language
 
week-4x
week-4xweek-4x
week-4x
 
C program to implement linked list using array abstract data type
C program to implement linked list using array abstract data typeC program to implement linked list using array abstract data type
C program to implement linked list using array abstract data type
 
Bcsl 033 data and file structures lab s3-3
Bcsl 033 data and file structures lab s3-3Bcsl 033 data and file structures lab s3-3
Bcsl 033 data and file structures lab s3-3
 
Linear search
Linear searchLinear search
Linear search
 
SATySFiのこれからの課題たち
SATySFiのこれからの課題たちSATySFiのこれからの課題たち
SATySFiのこれからの課題たち
 
Document
DocumentDocument
Document
 
Examples sandhiya class'
Examples sandhiya class'Examples sandhiya class'
Examples sandhiya class'
 
week-6x
week-6xweek-6x
week-6x
 
C & Python Introduction
C & Python IntroductionC & Python Introduction
C & Python Introduction
 
Avl tree
Avl treeAvl tree
Avl tree
 
C programs Set 2
C programs Set 2C programs Set 2
C programs Set 2
 
Double linked list
Double linked listDouble linked list
Double linked list
 
Computer programing w
Computer programing wComputer programing w
Computer programing w
 
New microsoft word document
New microsoft word documentNew microsoft word document
New microsoft word document
 
cosc 281 hw2
cosc 281 hw2cosc 281 hw2
cosc 281 hw2
 

Viewers also liked

腾讯大讲堂28 用户与产品的桥梁
腾讯大讲堂28 用户与产品的桥梁腾讯大讲堂28 用户与产品的桥梁
腾讯大讲堂28 用户与产品的桥梁
George Ang
 
2011 03 auto.sk
2011 03 auto.sk2011 03 auto.sk
2011 03 auto.sk
OneClick
 
Com scorewhithertheclickeurope0309
Com scorewhithertheclickeurope0309Com scorewhithertheclickeurope0309
Com scorewhithertheclickeurope0309
SHIJU VARGHESE
 
基于Eucalyptus的教育知识服务体系模型研究
基于Eucalyptus的教育知识服务体系模型研究基于Eucalyptus的教育知识服务体系模型研究
基于Eucalyptus的教育知识服务体系模型研究
liangxiao0315
 
Hadoop @ Sara & BiG Grid
Hadoop @ Sara & BiG GridHadoop @ Sara & BiG Grid
Hadoop @ Sara & BiG Grid
Evert Lammerts
 
использование свободного программного обеспечения в ВУЗах Украины
использование свободного программного обеспечения в ВУЗах Украиныиспользование свободного программного обеспечения в ВУЗах Украины
использование свободного программного обеспечения в ВУЗах Украины
Транслируем.бел
 
腾讯大讲堂36 竞争情报入门
腾讯大讲堂36 竞争情报入门腾讯大讲堂36 竞争情报入门
腾讯大讲堂36 竞争情报入门
George Ang
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
Amit Kapoor
 
Open Source Бизнес или о том как открыть 90% и остаться со штанами
Open Source Бизнес или о том как открыть 90% и остаться со штанамиOpen Source Бизнес или о том как открыть 90% и остаться со штанами
Open Source Бизнес или о том как открыть 90% и остаться со штанами
Транслируем.бел
 

Viewers also liked (18)

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

Similar to week-21x

Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
Azhar Javed
 
Sorting programs
Sorting programsSorting programs
Sorting programs
Varun Garg
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manual
SANTOSH RATH
 
Write a program (any language) to randomly generate the following se.pdf
Write a program (any language) to randomly generate the following se.pdfWrite a program (any language) to randomly generate the following se.pdf
Write a program (any language) to randomly generate the following se.pdf
archanaemporium
 

Similar to week-21x (20)

DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C Programs
 
Pnno
PnnoPnno
Pnno
 
Merge sort in CSolution#include-stdio-h- #define MAX 50 void mergeSort.docx
Merge sort in CSolution#include-stdio-h- #define MAX 50 void mergeSort.docxMerge sort in CSolution#include-stdio-h- #define MAX 50 void mergeSort.docx
Merge sort in CSolution#include-stdio-h- #define MAX 50 void mergeSort.docx
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
C programs
C programsC programs
C programs
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
Data Structure in C Programming Language
Data Structure in C Programming LanguageData Structure in C Programming Language
Data Structure in C Programming Language
 
Sorting programs
Sorting programsSorting programs
Sorting programs
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manual
 
Double linked list
Double linked listDouble linked list
Double linked list
 
week-20x
week-20xweek-20x
week-20x
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab files
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
Write a program (any language) to randomly generate the following se.pdf
Write a program (any language) to randomly generate the following se.pdfWrite a program (any language) to randomly generate the following se.pdf
Write a program (any language) to randomly generate the following se.pdf
 
Single linked list
Single linked listSingle linked list
Single linked list
 

More from KITE www.kitecolleges.com (20)

PPT (2)
PPT (2)PPT (2)
PPT (2)
 
week-1x
week-1xweek-1x
week-1x
 
ch14
ch14ch14
ch14
 
ch16
ch16ch16
ch16
 
holographic versatile disc
holographic versatile discholographic versatile disc
holographic versatile disc
 
week-22x
week-22xweek-22x
week-22x
 
week-16x
week-16xweek-16x
week-16x
 
week-5x
week-5xweek-5x
week-5x
 
week-3x
week-3xweek-3x
week-3x
 
ch8
ch8ch8
ch8
 
Intro Expert Systems test-me.co.uk
Intro Expert Systems test-me.co.ukIntro Expert Systems test-me.co.uk
Intro Expert Systems test-me.co.uk
 
ch17
ch17ch17
ch17
 
ch4
ch4ch4
ch4
 
week-7x
week-7xweek-7x
week-7x
 
week-9x
week-9xweek-9x
week-9x
 
week-14x
week-14xweek-14x
week-14x
 
AIRBORNE
AIRBORNEAIRBORNE
AIRBORNE
 
ch2
ch2ch2
ch2
 
week-23x
week-23xweek-23x
week-23x
 
week-2x
week-2xweek-2x
week-2x
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Recently uploaded (20)

Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 

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