COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
sort.cpp File Reference
#include <iostream>
#include <iomanip>
Include dependency graph for sort.cpp:

Go to the source code of this file.

Functions

int main ()
 
void print_array (const int[], int)
 
void sort (int[], int)
 
void swap (int &, int &)
 

Function Documentation

◆ main()

int main ( void )

Definition at line 10 of file sort.cpp.

11{
12 const int arraySize = 6; // size of array
13 int a[ arraySize ] = {-2, 7, 0, 23, 2048, -46}; // declare array a
14
15 cout << "Original array:\n";
16 print_array( a, arraySize );
17
18 sort( a, arraySize );
19
20 cout << "Sorted array:\n";
21 print_array( a, arraySize );
22
23 return 0;
24}
void sort(int[], int)
Definition sort.cpp:40
void print_array(const int[], int)
Definition sort.cpp:27

References print_array(), and sort().

Here is the call graph for this function:

◆ print_array()

void print_array ( const int array[],
int sizeOfArray )

Definition at line 27 of file sort.cpp.

28{
29 for ( int i = 0; i < sizeOfArray; ++i )
30 cout << "[" << setw(2) << i << "] ";
31 cout << endl;
32
33 for ( int i = 0; i < sizeOfArray; ++i )
34 cout << setw(3) << array[i] << " ";
35 cout << endl;
36}

Referenced by main().

Here is the caller graph for this function:

◆ sort()

void sort ( int array[],
int sizeOfArray )

Definition at line 40 of file sort.cpp.

41{
42 int i, j, idx;
43 int min;
44
45 for ( i = 0; i < sizeOfArray; ++i )
46 {
47 min = array[i];
48 idx = i;
49 for ( j = i + 1; j < sizeOfArray; ++j )
50 {
51 if ( array[j] < min )
52 {
53 min = array[j];
54 idx = j;
55 }
56 }
57 if ( idx != i )
58 swap( array[i], array[idx] ); // swap values
59 }
60}
void swap(int &, int &)
Definition sort.cpp:62

References swap().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ swap()

void swap ( int & a,
int & b )

Definition at line 62 of file sort.cpp.

63{
64 int tmp = a;
65 a = b;
66 b = tmp;
67 return;
68}

Referenced by sort().

Here is the caller graph for this function: