Chili Auction — Algorithms

Sort and search the sacks at Mbak Nurul's chili auction: merge, quick, heap, counting sort and KMP. 16 levels, right in the browser.

What the levels ask

  1. The chili harvest begins. Eight sacks are weighed, and the auction board must show their weights from the lightest. Complete the sort call in urutkanBerat so the numbers really come out in order as numbers.
  2. Nenek Marni's cart and Pak Tono's arrive together. The board uses merge sort: the line is split in two, each half is sorted, then merge joins them. Complete the last line of merge so that every sack reaches the board, each once.
  3. The auction's old rule: two sacks of the same weight are auctioned in the order they came. Pak Tono came before Bu Asih, and their 18 kg sacks weigh the same. Complete the condition in merge so the board stays in order of weight and does not swap the arrival order of sacks that weigh the same.
  4. Pak Tono's farmers' group weighed in its own store first, so today's 300 sacks arrive already sorted, lightest first. The board now uses quick sort, and Mbak Nurul's phone can only handle a recursion depth of 40 and at most 4,000 comparisons. Choose the pivot that is moved to the end before partitioning.
  5. The cooperative sends standard sacks: 300 of them, every one 25 kg. The day after is an ordinary day again. Mbak Nurul's phone still only handles a depth of 40, and on an ordinary day the board may write to the line at most 2,000 times. Choose how quickSort divides.
  6. The board tries heap sort: the sacks are arranged as a heap, the heaviest at the top, then taken one by one to the end of the line. The turun function moves a sack down from the top until both its children are lighter. Complete the right child's comparison so that the parent is always swapped with the heaviest of the parent and its two children.
  7. Today 500 sacks arrive already sorted, lightest first. Before sorting, bangunHeap arranges them all into a heap, and Mbak Nurul's phone gives that step at most 1,000 comparisons, two per sack. Choose how to build the heap.
  8. Bu Asih feels her sacks always shrink. Mbak Nurul records each sack's difference: the weight on the auction's scale minus the farmer's own note, from -3 to 4 kg. The board sorts with counting sort. Complete the index offset so every difference has its own box in hitung and every sack reaches the board.
  9. Yesterday's differences are right now, but Pak Tono and Nenek Marni complain: their sacks with the same difference show in reverse arrival order. Choose the direction of the loop that places sacks into hasil, so sacks with the same difference stay in arrival order.
  10. Cak Rohim, who drives the truck to the city's wholesale market, reads the auction board on his boss's iPhone, while Mbak Nurul and the farmers use Android phones. The board must be in order from the lightest on all of those phones. Complete the sort comparator in urutkanLot.
  11. Cak Rohim's boss wants to know the seven days in a row with the most chili picked this month, to schedule the big truck. tujuhHariTerbaik slides a seven-day window from the start of the month, and the phone may read each day at most twice. Complete the day that leaves the window when it slides by one day.
  12. Chili spoils fast. Cak Rohim's truck needs 225 kg, and Mbak Nurul wants to know the fewest days in a row whose harvest reaches that load. The window grows to the right, then shrinks from the left. Complete its shrinking condition.

And more, 16 levels in all.