We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Quicksort 1 - Partition
Quicksort 1 - Partition
+ 0 comments php:
$pivot = $arr[0]; $n = count($arr); $left = $right = $equal = []; for($i=0;$i<$n;$i++){ if($arr[$i] == $pivot){ $equal[] = $arr[$i]; }else if($arr[$i] < $pivot){ $left[] = $arr[$i]; }else{ $right[] = $arr[$i]; } } $result = array_merge($left,$equal,$right); return $result;
+ 0 comments Java 8:
public static List<Integer> quickSort(List<Integer> arr) { Collections.sort(arr); return arr; }
+ 0 comments Python 3
from itertools import tee, chain def quickSort(arr): equal = arr[0] t1, t2 = tee(arr) left = filter(lambda x: x < equal, t1) right = filter(lambda x: x > equal, t2) return chain(left, [equal], right)
+ 0 comments Swift
func quickSort(arr: [Int]) -> [Int] { var leftArr: [Int] = [] var equalArr: [Int] = [] var rigthArr: [Int] = [] let checkValue = arr[0] for item in arr { if item < checkValue { leftArr.append(item) } else if checkValue == item { equalArr.append(item) } else { rigthArr.append(item) } } return leftArr + equalArr + rigthArr }
+ 0 comments public static List<Integer> quickSort(List<Integer> arr) { List<Integer> left = new ArrayList<>(); List<Integer> equal = new ArrayList<>(); List<Integer> right = new ArrayList<>(); int p = arr.get(0); for(int i = 0; i < arr.size(); i++) { int element = arr.get(i); if(element < p) { left.add(element); } else if (element > p) { right.add(element); } else { equal.add(element); } } left.addAll(equal); left.addAll(right); return left; }
Load more conversations
Sort 344 Discussions, By:
Please Login in order to post a comment