//Author: Frank R. Mendez
//Title: Insertion Sort
//Description: Accepts int elements and stores it in an array list then sort every element in accending order
//Date: 7/13/13 5:53 PM
#include <iostream>
using namespace std;
class Sort {
public:
void insertionSort(int x[],int lenght);
void driver();
void display();
int arr[15]; // because insertion sort is less efficient with large list
int size;
int i,x,y,z,key;
};
void Sort::driver() {
cout<<"Enter size of list:" << endl;
cin>>size;
cout<<"Enter " << size << " values" << endl;
cout << endl;
for(y = 0; y < size; y++) {
cout << "Value [" << y << "]:";
cin>>arr[y];
}
cout << "Elements of your unsorted list: [ ";
for(y = 0; y < size; y++){
cout << arr[y] << " " ;
}
cout << "]" << endl;;
insertionSort(arr,size);
cout << "Elements of your sorted list: [ ";
for(z = 0; z < size; z++) {
cout << arr[z] << " ";
}
cout << "]";
}
void Sort::insertionSort(int x[], int length) {
for(int j =1; j < length; j++){
key = x[j];
i=j-1;
while(x[i]>key && i>=0){
x[i+1]=x[i];
i--;
}
x[i+1]=key;
}
}
int main () {
Sort frank;
frank.driver();
}
frankleslie8311 0 Newbie Poster
kvahanyan 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.