Skip to content

Commit 8fd389a

Browse files
committed
remove choose_pivot function from generic.h
adding a more generic interval RANDOM() function
1 parent 92ec281 commit 8fd389a

File tree

5 files changed

+11
-14
lines changed

5 files changed

+11
-14
lines changed

include/generic.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#define Max(a, b) ( (a > b) ? a : b )
2424
#define Min(a, b) ( (a < b) ? a : b )
2525

26+
#define RANDOM_INIT() srand(time(NULL))
27+
#define RANDOM(L, R) (L + rand() % ((R) - (L))) // gen a random integer in [L, R]
28+
2629
namespace alg
2730
{
2831
/**
@@ -48,16 +51,6 @@ namespace alg
4851
printf("\n");
4952
}
5053

51-
/**
52-
* select a random number between i and j
53-
*/
54-
static inline int choose_pivot(int i,int j)
55-
{
56-
assert(j>=i);
57-
int length = j - i;
58-
return i+(rand()%length);
59-
}
60-
6154
/**
6255
* pass in array with *len
6356
* return new length

include/quick_sort.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace alg
2828
template<typename T>
2929
static int __partition(T list[],int begin, int end)
3030
{
31-
int pivot_idx = choose_pivot(begin,end);
31+
int pivot_idx = RANDOM(begin,end);
3232
T pivot = list[pivot_idx];
3333
swap(list[begin], list[pivot_idx]);
3434

include/random_select.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#ifndef __RANDOM_SELECT_H__
1919
#define __RANDOM_SELECT_H__
2020

21+
#include <generic.h>
22+
2123
namespace alg
2224
{
2325
/**
@@ -26,7 +28,7 @@ namespace alg
2628
template<typename T>
2729
static int __partition(T list[],int begin, int end)
2830
{
29-
int pivot_idx = choose_pivot(begin,end);
31+
int pivot_idx = RANDOM(begin,end);
3032
T pivot = list[pivot_idx];
3133
swap(list[begin],list[pivot_idx]);
3234

src/quick_sort_demo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
int main()
99
{
10+
RANDOM_INIT();
1011
using namespace alg;
1112
const int MAX_ELEMENTS = 10;
1213
int list[MAX_ELEMENTS];

src/random_select_demo.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
int main()
99
{
10+
RANDOM_INIT();
1011
using namespace alg;
1112
const int MAX_ELEMENTS = 10;
1213
int list[MAX_ELEMENTS];
@@ -18,7 +19,7 @@ int main()
1819
list[i] = rand()%100;
1920
}
2021

21-
printf("The list before sorting is:\n");
22+
printf("The list is:\n");
2223
printlist(list,MAX_ELEMENTS);
2324

2425
// random-select k-th element
@@ -27,7 +28,7 @@ int main()
2728
int result = random_select(list,0,MAX_ELEMENTS-1,k);
2829

2930
// print the result
30-
printf("the %dth smallest number is %d:\n", k, list[result]);
31+
printf("random select the %dth smallest number %d:\n", k, list[result]);
3132

3233
return 0;
3334
}

0 commit comments

Comments
 (0)