bubsort - int temp; int input_size = input_vector.length();...

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

View Full Document Right Arrow Icon
// bubsort.cpp // An implementation of a bubble sort algorithm. / // compiler directives #include<iostream.h> #include"apvector.h" # // function prototypes void bubble_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); bubble_sort(nums); cout << "Sorted vector:\n"; display_array(nums); return 0; } // Bubble sort function. Sorts a vector of ints in descending order. { int i, j, flag = 1;
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 temp; int input_size = input_vector.length(); for(i = 1; (i &lt;= input_size) &amp;&amp; flag; i++) { flag = 0; for(j = 0; j &lt; (input_size - i); j++) { if (input_vector[j + 1] &gt; input_vector[j]) { temp = input_vector[j + 1]; input_vector[j + 1] = input_vector[j]; input_vector[j] = temp; flag = 1; } } } } // Function that simply displays each element of input_array. void display_array(apvector &lt;int&gt; input_vector) { int i; int input_size = input_vector.length(); for (i = 0; i &lt; input_size; i++) { cout &lt;&lt; input_vector[i] &lt;&lt; ' '; } cout &lt;&lt; &quot;\n\n&quot;; }...
View Full Document

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

Page1 / 2

bubsort - int temp; int input_size = input_vector.length();...

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