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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:45:44+00:00 2026-05-28T07:45:44+00:00

I have a list of about 200 integers whose values are between 1 and

  • 0

I have a list of about 200 integers whose values are between 1 and 5.

I want to get into learning about sorting algorithms and knowing where to apply each because at the moment I use bubble-sort for everything which I’ve been told is a terrible way to do things.

What would be the fastest sorting algorithm for this integer sorting?

EDIT: It turns out that because I know the numbers are 1 to 5 then I can use a bucket sort (?) algorithm which if I’m not mistaken – and I definitely could be – means that for each integer of value 1, I put it in the 1 group, value 2 I put it in the 2 group etc, then concatenate the groups at the end. This seems like a simple and efficient way to do it.

However since this is (currently) a learning excercise for me I am going to remove the 1 – 5 limitation and try to implement bubble-sort and merge-sort then compare the two to see which is faster.

Thanks for your help!

  • 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-28T07:45:44+00:00Added an answer on May 28, 2026 at 7:45 am

    … which I’ve been told is a terrible way to do things.

    First off, don’t accept as gospel anything you hear from random bods on the internet (even me).

    Bubble sort is fine under certain conditions, such as when the data is already mostly sorted, or the item count is relatively small (such as 200) (a), or you have no sort functionality built into the language and you’re on a tight deadline where lack of performance will annoy the customer but lack of functionality will get you fired 🙂

    This bias against bubble sort is similar to the “only one exit point from a function” and “no goto” rules. You should understand the reasoning behind them so that you know when the rules can be ignored safely.

    Anyway, on to the question proper. An efficient way for your specific case is to just count the items then output them, something like:

    dim count[1..5] = {0, 0, 0, 0, 0};
    for each item in list:
        count[item] = count[item] + 1
    for val in 1..5:
        for quant in 1..count[val]:
            output val
    

    That’s an O(n) time and O(1) space solution and you won’t find a more efficient big-O for a generalised sort routine – it’s only possible in this case because of the extra information you have about the data (limited to the values 1 through 5).

    If you wanted to examine all the different sort algorithms, the Wikipedia Sorting Algorithm page is a useful starting point, including the major algorithms and their properties.


    (a) As an aside, the following code (using worst case data for bubble sort), when run under CygWin on a not-very-powerful IBM T60 (2GHz dual core) laptop, completes in, on average, 0.157 seconds (5 samples: 0.150, 0.125, 0.192, 0.199, 0.115).

    I wouldn’t use it for sorting a million items (everyone knows bubble sort scales poorly) but 200 should be fine in most cases:

    #include <stdio.h>
    
    #define COUNT 200
    int main (void) {
        int i, swapped, tmp, item[COUNT];
    
        // Set up worst case (reverse order) data.
    
        for (i = 0; i < COUNT; i++)
            item[i] = 200 - i;
    
        // Slightly optimised bubble sort.
    
        swapped = 1;
        while (swapped) {
            swapped = 0;
            for (i = 1; i < COUNT; i++) {
                if (item[i-1] > item[i]) {
                    tmp = item[i-1];
                    item[i-1] = item[i];
                    item[i] = tmp;
                    swapped = 1;
                }
            }
        }
    
        // for (i = 0; i < COUNT; i++)
        //     printf ("%d ", item[i]);
        // putchar ('\n');
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have about 200 String values and each string needs to be placed into
I have about 10 drop down list controls that get populated. Instead of copying/pasting
I have some questions about the default values in a function parameter list Is
Recently I have been learning about WMI and WQL. I found out the list
I have a list of TV shows which is about 200 shows long. Each
Well it's not playing actually. I have a database with about 200 list of
I have a list of about 200,000 IP addresses. I would like to link
I have a list containing about 1000 file names to search under a directory
I have a question about list views. I hope someone knows the solution, because
I have a question about list in tcl, what is the correct way to

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.