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 7044311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:23:41+00:00 2026-05-28T02:23:41+00:00

question origin Given an unsorted array of size n containing objects with ids of

  • 0

question origin

Given an unsorted array of size n containing objects with ids of 0 … n-1, sort the array in place and in linear time. Assume that the objects contain large members such as binary data, so instantiating new copies of the objects is prohibitively expensive.

void linearSort(int* input, const int n) {
    for (int i = 0; i < n; i++) {
        while (input[i] != i) {
            // swap
            int swapPoint = input[i];
            input[i] = input[swapPoint];
            input[swapPoint] = swapPoint;
        }
    }
}

Is this linear? Does this sort work with any kind of array of ints? If so, why do we need quicksort anymore?

  • 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-05-28T02:23:42+00:00Added an answer on May 28, 2026 at 2:23 am

    Despite the while loop inside the for, this sort is linear O(n). If the while loop occurs multiple times for a given i then for the i values that match swapPoint there will not execute the while loop at all.

    This implementation will only work for arrays of ints where there are no duplicates and the values are sequential from 0 to n-1, which is why Quicksort still is relevant being O(n log n) because it works with non-sequential values.

    This can be easily tested by making the worst case:

    input = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
    

    and then using the following code:

    int whileCount = 0;
    for (int i = 0; i < n; i++)
    {
        while (input[i] != i)
        {
            whileCount++;
            // swap
            int swapPoint = input[i];
            input[i] = input[swapPoint];
            input[swapPoint] = swapPoint;
        }
        Console.WriteLine("for: {0}, while: {1}", i, whileCount);
    }
    

    The output will be as follows:

    for: 0, while: 9
    for: 1, while: 9
    for: 2, while: 9
    for: 3, while: 9
    for: 4, while: 9
    for: 5, while: 9
    for: 6, while: 9
    for: 7, while: 9
    for: 8, while: 9
    for: 9, while: 9
    

    so you see even in the worst case where you have the while loop run n-1 times in the first iteration of the for loop, you still only get n-1 iterations of the while loop for the entire process.

    Further examples with random data:

    {7, 1, 2, 4, 3, 5, 0, 6, 8, 9} => 2 on i=0, 1 on i=3 and nothing more. (total 3 while loop runs)
    {7, 8, 2, 1, 0, 3, 4, 5, 6, 9} => 7 on i=0 and nothing more (total 7 while loop runs)
    {9, 8, 7, 4, 3, 1, 0, 2, 5, 6} => 2 on i=0, 2 on i=1, 1 on i=2, 1 on i=3 (total 6 while loop runs)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Related question: why does Git send whole repository each time push origin master The
Question Can I build a image database/library that has an e-commerce style checkout system
Question says it all, really. My application is a time tracker. It's currently written
Question Using XSLT 1.0, given a string with arbitrary characters how can I get
Question Is there a way to have a method that will always run anytime
I wish to sort a space separated table , with the numerical value that
In response to a question about pulling one commit at a time from a
Basic question: How do I disassociate a git repo from the origin from which
Today I was writing some C code to sort an array of structs using
I see that the above question was asked earlier, however even after referring them

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.