-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble_sort_pointer2.cpp
More file actions
69 lines (61 loc) · 1.92 KB
/
Copy pathbubble_sort_pointer2.cpp
File metadata and controls
69 lines (61 loc) · 1.92 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
// bubble sort using call by reference
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
void bubble( int *, const int );
void change( int *, int * );
void bubble_2( int [], const int );
void change_2( int, int );
int main(){
srand(time ( NULL ));
int size;
cout << "enter size : " << endl;
cin >> size;
int array[ size ];
cout << "Original order : " << endl;
for ( int i = 0; i < size; i++){
array[ i ] = 1 + rand( ) % 100;
cout << array[ i ] << " ";
}
cout << "\n\nBubble sort order : " << endl;
bubble_2( array, size );
for ( int i = 0; i < size; i++)
cout << array[ i ] << " ";
return 0;
}
/* These functions are use pointers and fixed size variable.And changed function
must access in the bubble function's array's elemnts. For this reason we can
uses call by reference. If we were not used a pointer in these functions, we
cant access the elements of array..*/
void bubble( int *arr, const int size ){
//void change( int *, int * );
for ( int i = 0; i < size-1; i++){
for ( int j = 0; j < size-1; j++){
if ( arr[ j ] > arr[ j+1 ] )
change( &arr[ j ], &arr[ j+1 ]);
}
}
}
void change( int *e1_ptr, int *e2_ptr ){
int temp = *e1_ptr;
*e1_ptr = *e2_ptr;
*e2_ptr = temp;
}
/* These function are use a array and fixed size variable.And changed function
must access in the bubble function's array's elements. We did not use the
pointer in these functions.We could'nt access the elements of array. And we
couldn't set the array with order..*/
void bubble_2 ( int arr[], const int size ){
for ( int i = 0; i < size-1; i++){
for ( int j = 0; j < size-1; j++){
if ( arr[ j ] > arr[ j+1 ] )
change_2( arr[ j ], arr[ j+1 ]);
}
}
}
void change_2( int e1, int e2 ){
int temp = e1;
e1 = e2;
e2 = temp;
}