Tuesday, October 23, 2012

Probblem 1: Count Sort Problem

Problem 1: Given an array of 100000 elements with numbers 15, 25, 35, 45 repeating. No other numbers are in the array. Sort the array with minimal space n time.

Solution: Find number of 15s, 25s, 35s and 45s in 4 different counters -> a, b, c, d.
Sorted array = [ { a times 15} ; { b times 25 } ; { c times 35 } ; { d times 45 } ]
time = O(n) ; 
space = n + 4

No comments:

Post a Comment