|
| 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 | +} |
0 commit comments