inssort - int input_size = input_vector.length(); for (j =...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
// inssort.cpp // An implementation of an insertion sort algorithm. / // compiler directives #include<iostream.h> #include"apvector.h" # // function prototypes void insertion_sort(apvector <int> &input_vector); void display_array(apvector <int> input_vector); v int main() { apvector <int> nums(5); nums[0] = 20; nums[1] = 31; nums[2] = 17; nums[3] = 47; nums[4] = 14; cout << "Unsorted vector:\n"; display_array(nums); insertion_sort(nums); cout << "Sorted vector:\n"; display_array(nums); return 0; } // Insertion sort function. Sorts a vector of ints in descending order. { int j, i, key;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: int input_size = input_vector.length(); for (j = 1; j < input_size; j++) { key = input_vector[j]; // Move all values smaller then key up one position. for (i = j - 1; (i >= 0) && (input_vector[i] < key); i--) { input_vector[i + 1] = input_vector[i]; } input_vector[i + 1] = key; // insert key into proper position } } // Function that simply displays each element of input_vector. void display_array(apvector <int> input_vector) { int i; int input_size = input_vector.length(); for (i = 0; i < input_size; i++) { cout << input_vector[i] << ' '; } cout << "\n\n"; }...
View Full Document

This note was uploaded on 06/20/2011 for the course COMPUTING 379 taught by Professor Dr.anoopsarkar during the Spring '11 term at American College of Computer & Information Sciences.

Page1 / 2

inssort - int input_size = input_vector.length(); for (j =...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online