Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8731765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:18:46+00:00 2026-06-13T09:18:46+00:00

This is not my school home work. This is my own home work and

  • 0

This is not my school home work. This is my own home work and I am self-learning algorithms.

In Algorithm Design Manual, there is such an excise

4-25 Assume that the array A[1..n] only has numbers from {1, . . . , n^2} but that at most log log n of these numbers ever appear. Devise an algorithm that sorts A in substantially less than O(n log n).

I have two approaches:


The first approach:

Basically I want to do counting sort for this problem. I can first scan the whole array (O(N)) and put all distinct numbers into a loglogN size array (int[] K).

Then apply counting sort. However, when setting up the counting array (int[] C), I don’t need to set its size as N^2, instead, I set the size as loglogN too.

But in this way, when counting the frequencies of each distinct number, I have to scan array K to get that element’s index (O(NloglogN) and then update array C.


The second approach:

Again, I have to scan the whole array to get a distinct number array K with size loglogN.

Then I just do a kind of quicksort like, but the partition is based on median of K array (i.e., each time the pivot is an element of K array), recursively.

I think this approach will be best, with O(NlogloglogN).


Am I right? or there are better solutions?

Similar excises exist in Algorithm Design Manual, such as

4-22 Show that n positive integers in the range 1 to k can be sorted in O(n log k) time. The interesting case is when k << n.

4-23 We seek to sort a sequence S of n integers with many duplications, such that the number of distinct integers in S is O(log n). Give an O(n log log n) worst-case time algorithm to sort such sequences.

But basically for all these excises, my intuitive was always thinking of counting sort as we can know the range of the elements and the range is short enough comparing to the length of the whole array. But after more deeply thinking, I guess what the excises are looking for is the 2nd approach, right?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:18:48+00:00Added an answer on June 13, 2026 at 9:18 am

    We can just create a hash map storing each element as key and its frequency as value.

    Sort this map in log(n)*log(log(n)) time i.e (klogk) using any sorting algorithm.

    Now scan the hash map and add elements to the new array frequency number of times. Like so:

    total time = 2n+log(n)*log(log(n)) = O(n) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a school-related question, although not exactly homework. I'm taking an algorithms course,
Why does this not work? Do I not understand delegate covariance correctly? public delegate
I've done this before with inputs, why on earth does this not work? http://euroworker.no/user/checkout
in school we got this class file: class Konto: def __init__(self, nummer): self.__nr =
I am very new to webApp design and have entered this competition in school
This is not homework, I don't have money for school so I am teaching
My school uses 802.11a, but at home I use 802.11b. This means every time
I compiled this code at home on my mac w/ xcode and there was
I would like to know how could I resolve this problem(it's not a school
This is not homework, I don't have money for school so I am teaching

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.