-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting_function.cpp
More file actions
75 lines (65 loc) · 1.88 KB
/
Copy pathsorting_function.cpp
File metadata and controls
75 lines (65 loc) · 1.88 KB
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
65
66
67
68
69
70
71
72
73
74
75
/*
Using the pointer showing a sorting function having a lot of purpose.
*/
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
void rand_array ( int *, const int );
void print( int *, const int );
void bubble( int *, const int, int(*)(int, int) );
int increasing( int, int );
int decreasing( int, int );
int main(){
srand(time ( NULL ));
int choise;
const int SIZE = 10;
int array[ SIZE ];
cout << " Original order : ";
rand_array ( array, SIZE );
print ( array, SIZE );
cout << "\n\n Enter your choice 1(increase) - 2(decrease) : ";
cin >> choise;
switch ( choise ){
case 1 :
cout << "\n Increasing order : ";
bubble( array, SIZE, increasing );
print ( array, SIZE );
break;
case 2 :
cout << "\n Decreasing order : ";
bubble( array, SIZE, decreasing );
print ( array, SIZE );
break;
}
return 0;
}
void rand_array ( int *array, const int SIZE ){
for ( int i = 0; i < SIZE; i++)
array[ i ] = 1 + rand( ) % 100;
}
void print( int *array, const int SIZE ){
for ( int i = 0; i < SIZE; i++)
cout << array[ i ] << " " ;
}
void bubble( int *array, const int SIZE, int (*compare)( int, int ) ){
void change( int *, int * );
for ( int tour = 0; tour < SIZE; tour++){
for ( int i = 0; i < SIZE - 1; i++ ){
if ( (*compare)( array[ i ], array [ i + 1 ] ) )
change( &array[ i ], &array [ i + 1 ] );
}
}
}
void change( int *e1_ptr, int *e2_ptr ){
int temp;
temp = *e1_ptr;
*e1_ptr = *e2_ptr;
*e2_ptr = temp;
}
int increasing( int e1, int e2 ){
return e1 > e2; // if a is greater than b, replaced a and b.
}
int decreasing( int e1, int e2 ){
return e1 < e2; // if b is greater than a, replaced a and b.
}