id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

partition


This method focuses only on the PARTITIONING portion of the quick sort algorithm. Normally, the quick sort algorithm would choose a pivot, partition around that pivot, and then recursively call quick sort on the parts to the left and right of the pivot. Given an array of ints, partition that array using the array's last element as the "pivot", as you would for the quicksort algorithm, putting all elements less than or equal to the pivot on the left side of the array, and all the elements greater than the pivot on the right side of the array. Use the same algorithm discussed in class, moving from left to right. Whenever an element's value is less than or equal to the pivot, swap it with the "next" position on the left that has not been swapped. NOTE that this is not the only way to partition an array for quicksort. In general, any algorithm that picks a pivot and puts the appropriate numbers to the left and right of the pivot is acceptable. However, this problem checks to see if you can write the code for the particular algorithm described above.


partition([47, 2, 18, 6, 3, 59, 2, 8, 19, 5]) → [2, 3, 2, 5, 47, 59, 18, 8, 19, 6]
partition([15, 30, 21, 3, 13, 4, 9, 13]) → [3, 13, 4, 9, 13, 21, 15, 30]
partition([1, 3, 1, 3, 1, 3, 1, 3, 2]) → [1, 1, 1, 1, 2, 3, 3, 3, 3]

...Save, Compile, Run (ctrl-enter)

int[] partition(int[] nums) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 200

Copyright Nick Parlante 2017 - privacy