Skip to content

Commit 2d91d53

Browse files
authored
Merge pull request ashutosh97#134 from sakshamb2113/master
Add insertion sort using classes
2 parents e4b79ba + 4e500b0 commit 2d91d53

File tree

2 files changed

+189
-0
lines changed

2 files changed

+189
-0
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
/* Hi I am using my own class to use in insertion sort function*/
5+
6+
template<class Item>
7+
class LinearList{
8+
private:
9+
int MaxSize;
10+
Item *element; // 1D dynamic array
11+
int len;
12+
public:
13+
/* Default constructor.
14+
* Should create an empty list that not contain any elements*/
15+
LinearList()
16+
{
17+
element=nullptr;
18+
len=0;
19+
};
20+
21+
/* This constructor should create a list containing MaxSize elements. You can intialize the elements with any values.*/
22+
LinearList(const int& MaxListSize)
23+
{
24+
MaxSize=MaxListSize;
25+
element = new Item[MaxSize];
26+
len=0;
27+
28+
};
29+
30+
/* Destructor.
31+
* Must free all the memory contained by the list */
32+
~LinearList(){};
33+
34+
/* Indexing operator.
35+
* It should behave the same way array indexing does. i.e,
36+
* LinearList L;
37+
* L[0] should refer to the first element;
38+
* L[1] to the second element and so on.
39+
* */
40+
Item& operator[](const int& i)
41+
{
42+
return element[i];
43+
}; //return i'th element of list
44+
45+
/* Returns true if the list is empty, false otherwise.
46+
* */
47+
bool isEmpty()
48+
{
49+
if(len==0)
50+
{
51+
return true;
52+
}
53+
else
54+
{
55+
return false;
56+
}
57+
};
58+
59+
/* Returns the actual length (number of elements) in the list.
60+
* */
61+
int length()
62+
{
63+
return len;
64+
};
65+
66+
/* Returns the maximum size of the list.
67+
* */
68+
int maxSize()
69+
{
70+
return maxSize;
71+
};
72+
73+
/* Returns the k-th element of the list.
74+
* */
75+
Item returnListElement(const int k)
76+
{
77+
return element[k-1];
78+
};
79+
/* Set x to the k-th element and
80+
* return true if k-th element is present otherwise return false.
81+
* */
82+
bool find(const int k, Item& x)
83+
{
84+
if(x==element[k-1])
85+
{
86+
return true;
87+
}
88+
else
89+
{
90+
91+
return false;
92+
}
93+
};
94+
95+
/* Search for x and
96+
* return the position if found, else return 0.
97+
* */
98+
int search(Item& x)
99+
{
100+
for(int i=0;i<len;i++)
101+
{
102+
if(element[i]==x)
103+
{
104+
return i+1;
105+
}
106+
}
107+
return 0;
108+
109+
};
110+
111+
/* Set x to the k-th element and delete that k-th element.
112+
* */
113+
void deleteElement(const int k, Item& x)
114+
{
115+
x=element[k-1];
116+
for(int i=k;i<len;i++)
117+
{
118+
element[i-1]=element[i];
119+
}
120+
len=len-1;
121+
};
122+
123+
/* Insert x after k-th element.
124+
* */
125+
void insert(const int k, Item& x)
126+
{
127+
for(int i=len;i>k;i--)
128+
{
129+
element[i]=element[i-1];
130+
}
131+
element[k]=x;
132+
len=len+1;
133+
};
134+
135+
136+
};
137+
138+
//insertion sort function
139+
void insertionSort(LinearList<int>& A)
140+
{
141+
int i,j,temp,len;
142+
i=j=0;
143+
len=A.length();
144+
j=len;
145+
for(i=1;i<len;i++)
146+
{
147+
temp=A[i];
148+
for(j=i-1;j>-1;j--)
149+
{
150+
if(temp<A[j])
151+
{
152+
A[j+1]=A[j];
153+
}
154+
else
155+
{
156+
break;
157+
}
158+
}
159+
A[j+1]=temp;
160+
161+
}
162+
}
163+
164+
//Driver Function
165+
int main()
166+
{
167+
int n,temp,i;
168+
cin >> n;
169+
LinearList<int> l(100);
170+
171+
for(i=0;i<n;i++)
172+
{
173+
cin >> temp;
174+
l.insert(i,temp);
175+
}
176+
insertionSort(l);
177+
for(i=0;i<n;i++)
178+
{
179+
cout << l[i] << " ";
180+
}
181+
cout << endl;
182+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
I have made an insertion sort algorithm in c++.
2+
This program uses a class as its data structure instead of array.
3+
Input:-
4+
first line will be the number of elements to be sorted
5+
second line will contain n space separated elements
6+
Output:-
7+
The program will use insertion sort algorithm to output n space separated numbers in sorted order.

0 commit comments

Comments
 (0)