-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment 12.cpp
More file actions
86 lines (85 loc) · 1.75 KB
/
Assignment 12.cpp
File metadata and controls
86 lines (85 loc) · 1.75 KB
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
//============================================================================
// Name : Assignment 12.cpp
// Author : 21258
// Version :
// Copyright : Your copyright notice
// Description : Sorting algorithms (Insertion,Shell)
//============================================================================
#include <iostream>
using namespace std;
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i-1;
while (j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
int shellSort(int arr[], int n)
{
for (int gap = n/2; gap > 0; gap /= 2)
{
for (int i = gap; i < n; i += 1)
{
int temp = arr[i];
int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
arr[j] = arr[j - gap];
arr[j] = temp;
}
}
return 0;
}
void print(int *a, int n)
{
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
int main() {
int a[100],n,e;
char c;
do
{
cout<<"Enter the number of elements: ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>a[i];
}
cout<<"Your list is: "<<endl;
print(a,n);
cout<<"Enter your choice: \n1.Insertion Sort\n2.Shell Sort\n";
cin>>e;
switch(e)
{
case 1:
insertionSort(a,n);
cout<<"Sorted list is: "<<endl;
print(a,n);
break;
case 2:
shellSort(a,n);
cout<<"Sorted list is: "<<endl;
print(a,n);
break;
default:
cout<<"Invalid option!"<<endl;
}
cout<<"Continue?";
cin>>c;
}
while(c=='Y'||c=='y');
cout<<"Exit!";
return 0;
}