-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
QuickSort.sol
43 lines (38 loc) · 1.2 KB
/
QuickSort.sol
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
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
/**
* @title sort given array using Quick sort.
* @author [Priyda](https://github.com/priyda)
* @dev Contract to demonstrate how quick sort is working.
*/
contract QuickSort {
function sort(uint256[] memory data) public returns (uint256[] memory) {
quickSort(data, int256(0), int256(data.length - 1));
return data;
}
/** Quicksort is a sorting algorithm based on the divide and conquer approach **/
function quickSort(
uint256[] memory _arr,
int256 left,
int256 right
) internal {
int256 i = left;
int256 j = right;
if (i == j) return;
uint256 pivot = _arr[uint256(left + (right - left) / 2)];
while (i <= j) {
while (_arr[uint256(i)] < pivot) i++;
while (pivot < _arr[uint256(j)]) j--;
if (i <= j) {
(_arr[uint256(i)], _arr[uint256(j)]) = (
_arr[uint256(j)],
_arr[uint256(i)]
);
i++;
j--;
}
}
if (left < j) quickSort(_arr, left, j);
if (i < right) quickSort(_arr, i, right);
}
}