#include <iostream>
#include <vector>
using namespace std;
class insertionsort{
private:
int a[100];
int n;
public:
insertionsort(int arr[], int size){
n = size;
int j, key;
for (int i=0; i < n; i++) {
a[i] = arr[i];
}
for(int i = 1; i<size; i++) {
key = a[i];
j = i;
while( j > 0 && a[j-1]>key) {
a[j] = a[j-1];
j--;
}
a[j] = key;
}
}
void display() {
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << endl;
}
};
int main() {
int size;
cout << "Enter the size of the 1-d array: ";
cin >> size;
int arr[size];
cout << "Enter all the elemets of the array with spaces:\n";
for(int i=0; i<size; i++){
cin>>arr[i];
}
insertionsort is(arr, size);
is.display();
return 0;
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter