How to print vector after sorting

Im having trouble with a c++ assignment:

Write a program that performs a sorting routine as discussed in class.
Create an array of 10 integers
(e.g., 63, 42, 27, 111, 55, 14, 66, 9, 75, 87).
Pass this array to a separate function that sorts the integers and returns a vector containing the sorted integers.
Use either the SELECTIONSORT algorithm or the INSERTIONSORT


I've created the array and then insertion sorted it, but how do I put it into/return a vector and then print that vector?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

#include <cstdlib>
#include <iostream>
#include <vector>
 
using namespace std;
 
//member function
void insertion_sort(int array[], int length);
void print_array(int array[],int size);
 void print_vector(vector<int>& my_vector);
  
int main() 

{

int i;

int my_array[10]= {63, 42, 27, 111, 55, 14,  66, 9, 75, 87};

vector<int> my_vector;

insertion_sort(my_array,10);

 return 0;
}
 
void insertion_sort(int array[], int length) {
 int x,y,z; 
 for (x = 0; x < length; x++) {
 y = x;
 while (y > 0 && array[y - 1] > array[y]) {
 z = array[y];
 array[y] = array[y - 1];
 array[y - 1] = z;
 y--;
 }
 print_array(array,10);
 }//end of for loop
 

 }//end of insertion_sort.
 
void print_array(int array[], int size)

{
  
  
 cout<< "Please wait while sorting " << ": ";
 int s;
 for (s=0; s<size;s++)
 for (s=0; s<size;s++)
 cout <<" "<< array[s];
 
 cout << endl;
 }//end of print_array
 


 void print_vector(vector<int>& my_vector)
{
   }   
 
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/reference/algorithm/sort/
Topic archived. No new replies allowed.