1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
#include<iostream>
#include<time.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
int main()
void()
{
void merge(int,int,int);
void merge_sort(int low,int high);
int pick;
char data;
cout<< "Choices:\n";
cout<< "1. Insertion\n";
cout<< "2. Selection\n";
cout<< "3. Merge\n";
cout<< "4. Bubble\n";
cout<< "5. Exit\n";
cout<< "Enter the Number of your Choice: ";
cin>> pick;
switch(pick)
{
case 1:
int i,j,n,temp,a[30];
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("\nEnter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n-1;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("\nSorted list is as follows\n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
break;
case 2:
int q,w,e,loc,temmp,min,d[30];
cout<<"Enter the number of elements: ";
cin>>e;
cout<<"\nEnter the elements: ";
for(q=0;q<e;q++)
{
cin>>d[q];
}
for(q=0;q<e-1;q++)
{
min=d[q];
loc=q;
for(w=q+1;w<e;w++)
{
if(min>d[w])
{
min=d[w];
loc=w;
}
}
temmp=d[q];
d[q]=d[loc];
d[loc]=temmp;
}
cout<<"\nSorted list is as follows\n";
for(q=0;q<e;q++)
{
cout<<d[q]<<" ";
}
break;
case 3:
int am[50];
{
int mid;
if(low<high)
{
mid = low + (high-low)/2; //This avoids overflow when low, high are too large
merge_sort(low,mid);
merge_sort(mid+1,high);
merge(low,mid,high);
}
}
void merge(int low,int mid,int high)
{
int h,i,j,bm[50],k;
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
{
if(am[h]<=am[j])
{
bm[i]=am[h];
h++;
}
else
{
bm[i]=am[j];
j++;
}
i++;
}
if(h>mid)
{
for(k=j;k<=high;k++)
{
bm[i]=am[k];
i++;
}
}
else
{
for(k=h;k<=mid;k++)
{
bm[i]=am[k];
i++;
}
}
for(k=low;k<=high;k++) am[k]=bm[k];
}
int main()
{
int num,i;
cout<<"*****"<<endl;
cout<<" merge sort"<<endl;
cout<<"*****"<<endl;
cout<<endl<<endl;
cout<<"Please Enter THE NUMBER OF ELEMENTS you want to sort:"<<endl;
cin>>num;
cout<<endl;
cout<<"Now, Please Enter the ( "<< num <<" ) numbers (ELEMENTS):"<<endl;
for(i=1;i<=num;i++)
{
cin>>am[i] ;
}
merge_sort(1,num);
cout<<endl;
cout<<"So, the sorted list (using MERGE SORT) will be :"<<endl;
cout<<endl<<endl;
for(i=1;i<=num;i++)
cout<<am[i]<<" ";
cout<<endl<<endl<<endl<<endl;
return 1;
break;
case 4:
int b[50],c,u,p,tempp;
cout<<"Enter the size of array: ";
cin>>c;
cout<<"Enter the array elements: ";
for(u=0;u<c;++u)
cin>>b[u];
for(u=1;u<c;++u)
{
for(p=0;p<(c-u);++p)
if(b[p>b[p+1]])
{
tempp=b[p];
b[p]=b[p+1];
b[p+1]=tempp;
}
}
cout<<"Array after bubble sort:";
for(u=0;u<c;++u)
cout<<" "<<b[u];
break;
case 5:
cout <<"Press Enter to Exit";
default:
if(data > 5)
{
cout<<"Sorry, you've entered the wrong choice. Please try again.";
cout<<"\nYour Choice: ";
cin>> pick;
}
return 0;
}
return 0;
}
}
|