-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom.cpp
More file actions
137 lines (85 loc) · 1.92 KB
/
random.cpp
File metadata and controls
137 lines (85 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include<iostream>
#include<ctime>
#include<stdlib.h>
using namespace std;
struct link {
int value;
struct link * next;
struct link * prev;
};
void addLink(struct link* bsent, int val){
struct link * new_link = new struct link;
new_link->value = val;
bsent->prev->next = new_link;
new_link->prev = bsent->prev;
bsent->prev = new_link;
new_link->next = 0;
}
bool getSum(int *array, int length, int sum){
int end = length - 1;
int begin = 0;
while(begin != end){
if(array[begin] + array[end] < sum){
begin++;
}
else if(array[begin] + array[end] > sum){
end--;
}
if(sum == array[begin] + array[end])
return true;
}
return false;
}
void sort_array(int * array, int begin, int end){
if(begin >= end)
return;
int pivot = (begin + end) / 2;
int bindex = begin;
int eindex = end;
while(bindex <= eindex){
if(array[bindex] >= array[pivot] && array[eindex] <= array[pivot]){
int temp = array[bindex];
array[bindex] = array[eindex];
array[eindex] = temp;
bindex++;
eindex--;
}
else if(array[bindex] >= array[pivot]){
eindex--;
}
else if(array[eindex] <= array[pivot]){
bindex++;
}
else{
bindex++;
eindex++;
}
}
sort_array(array, begin, eindex);
sort_array(array, bindex, end);
}
int main() {
int length = 10;
int *array = new int[length];
srand(time(NULL));
for(int i = 0; i < length; i++){
array[i] = (rand() % 10);
cout << array[i] << ", ";
}
cout << endl;
sort_array(array, 0, length - 1);
cout << "Sorted Array! :" << endl;
for(int i = 0; i < length; i++){
cout << array[i] << ", ";
}
cout << "\n" << endl;
cout << getSum(array, length, 10) << endl;
/*
struct link *sent1 = new link;
struct link *sent2 = new link;
sent1->next = sent2;
sent1->prev = 0;
sent2->prev = sent1;
sent2->next = 0;
*/
}