-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuicksort.cpp
More file actions
69 lines (50 loc) · 1.22 KB
/
Copy pathQuicksort.cpp
File metadata and controls
69 lines (50 loc) · 1.22 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
//
// main.cpp
// lab04.5
//
// Created by Andres Rios on 3/2/19.
// Copyright © 2019 Andres Rios. All rights reserved.
//final edit Friday: 2:47pm
// coded with pesudo code from book and from lecture(structure for partition and quicksort) notes(randompartition)
#include <iostream>
using namespace std;
int part(int arr[], int p, int r){
int x = arr[r];
int i = (p - 1);
for(int j =p;j<=r-1;j++){
if(arr[j]<=x){
i++;
swap(arr[i],arr[j]);
}
}
swap(arr[i+1],arr[r]);
return (i+1);
}
int randompartition(int arr[],int p,int r){
//random function help from cplusplus library
int random = p+ rand() % (r - p);
//collaborated with cristian ortiz with coming up with a workable random int
swap(arr[r],arr[random]);
return part(arr,p,r);
}
void quicksort(int arr[], int p, int r){
if (p<r){
int q = randompartition(arr,p,r);
quicksort(arr,p,q-1);
quicksort(arr,q+1,r);
}
}
int main() {
int size;
// cout<<endl;
cin>>size;
int arr[size];
for(int i = 0; i<size;i++){
cin>>arr[i];
}
quicksort(arr, 0, size-1);
for(int i = 0;i<size;i++){
cout<<arr[i]<<";";
}
return 0;
}