0% found this document useful (0 votes)
18 views

Wap For Sorting A List of Numbers and Names Using Insertion, Selection, Quick and Merge Sorting Techniques

This document provides code to sort a list of numbers and names using insertion sort, selection sort, quick sort, and merge sort algorithms in C++. It includes functions for selection sort, insertion sort, and bubble sort that take an array and size as parameters, sort the array using the specified algorithm, and output the sorted array. The main function allows the user to enter a number of elements, populate an array, choose a sorting algorithm, call the corresponding sorting function, and output the sorted array.

Uploaded by

jayesh_meena
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Wap For Sorting A List of Numbers and Names Using Insertion, Selection, Quick and Merge Sorting Techniques

This document provides code to sort a list of numbers and names using insertion sort, selection sort, quick sort, and merge sort algorithms in C++. It includes functions for selection sort, insertion sort, and bubble sort that take an array and size as parameters, sort the array using the specified algorithm, and output the sorted array. The main function allows the user to enter a number of elements, populate an array, choose a sorting algorithm, call the corresponding sorting function, and output the sorted array.

Uploaded by

jayesh_meena
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

WAP FOR SORTING A LIST OF NUMBERS AND NAMES USING INSERTION, SELECTION, QUICK AND MERGE SORTING TECHNIQUES.

#include<iostream.h> #include<conio.h> #include<stdio.h> #include<limits.h> void selsort(int [],int) ; void inssort(int [],int) ; void bubblesort(int [],int); void main() { clrscr(); int ar[50],n,k; char ch; cout<<"how many elements u want to create array with\n"; cin>>n; cout<<"\nenter array elements\n"; for(int i=0;i<n;i++) cin>>ar[i] ; cout<<"enter 's' for selection sort snd 'i' for insertion sorting and 'b' for buuble sort\n\n "; cin>>ch; switch(ch) { case 's':selsort(ar,n); break; case 'i':inssort(ar,n); break; case 'b':bubblesort(ar,n); break; } cout<<"array after sorting is\n"; for( i=0;i<n;i++) { cout<<ar[i]<<" " ; } getch(); } void selsort(int ar[],int n) { int small,pos,tmp,i,j; for(i=0;i<n;i++) { small=ar[i]; for(j=i+1;j<n;j++) {if(ar[j]<small) {small=ar[j];pos=j; } }

tmp=ar[i]; ar[i]=ar[pos]; ar[pos]=tmp ; cout<<"\n array after "<<i+1<<"iteration for(j=0;j<n;j++) cout<<ar[j]<<" "; } } void inssort(int ar[],int n) { int tmp,j; ar[0]=INT_MIN; for(int i=0;i<=n;i++) { tmp=ar[i]; j=i-1; while(tmp<=ar[j]) { ar[j+1]=ar[j]; j--; } ar[j+1]=tmp; cout<<"array after pass-"<<i<<"-is:"; for(int k=0;k<=n;k++) cout<<ar[k]<<" "; cout<<endl; } } void bubblesort(int ar[],int n) { int tmp,ctr=0; for(int i=0;i<n;i++) {for (int j=0;j<(n-1)-i;j++) { if(ar[j]>ar[j+1]) {tmp=ar[j]; ar[j]=ar[j+1]; ar[j+1]=tmp; } } cout<<"array after iteration-"<<++ctr<<"-is"; for(int k=0;k<n;k++) cout<<ar[k]<<" "; cout<<endl; } }

\t

";

You might also like