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.
My Java solution with o(n) time complexity and o(n) space complexity:
publicstaticList<Integer>quickSort(List<Integer>arr){if(arr.size()==1)returnarr;//already sortedintpivot=arr.get(0);// pivot is always arr[0]//declare each subarrayList<Integer>left=newArrayList<>();List<Integer>right=newArrayList<>();List<Integer>equal=newArrayList<>();//partition the og arrfor(intelement:arr){if(element<pivot)left.add(element);elseif(element>pivot)right.add(element);elseequal.add(element);}//combine the subarraysList<Integer>combinedList=Stream.of(left,equal,right).flatMap(List::stream).collect(Collectors.toList());returncombinedList;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Quicksort 1 - Partition
You are viewing a single comment's thread. Return to all comments →
My Java solution with o(n) time complexity and o(n) space complexity: