FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Explore-open-source/QuickSort.cpp at main · coder2hacker/Explore-open-source · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
coder2hacker
/
Explore-open-source
Public
Notifications
You must be signed in to change notification settings
Fork
355
Star
57
Code
Issues
1
Pull requests
0
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
Explore-open-source
/
QuickSort.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
71 lines (51 loc) · 1.1 KB
Breadcrumbs
Explore-open-source
/
QuickSort.cpp
Copy path
File metadata and controls
71 lines (51 loc) · 1.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
C++ Implementation of the Quick Sort Algorithm.
#
include
<
iostream
>
using
namespace
std
;
int
partition
(
int
arr[],
int
start,
int
end)
{
int
pivot = arr[start];
int
count =
0
;
for
(
int
i = start +
1
; i <= end; i++) {
if
(arr[i] <= pivot)
count++;
}
//
Giving pivot element its correct position
int
pivotIndex = start + count;
swap
(arr[pivotIndex], arr[start]);
//
Sorting left and right parts of the pivot element
int
i = start, j = end;
while
(i < pivotIndex && j > pivotIndex) {
while
(arr[i] <= pivot) {
i++;
}
while
(arr[j] > pivot) {
j--;
}
if
(i < pivotIndex && j > pivotIndex) {
swap
(arr[i++], arr[j--]);
}
}
return
pivotIndex;
}
void
quickSort
(
int
arr[],
int
start,
int
end)
{
//
base case
if
(start >= end)
return
;
//
partitioning the array
int
p =
partition
(arr, start, end);
//
Sorting the left part
quickSort
(arr, start, p -
1
);
//
Sorting the right part
quickSort
(arr, p +
1
, end);
}
int
main
()
{
int
arr[] = {
9
,
3
,
4
,
2
,
1
,
8
};
int
n =
6
;
quickSort
(arr,
0
, n -
1
);
for
(
int
i =
0
; i < n; i++) {
cout << arr[i] <<
"
"
;
}
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL