Question

Write an algorithm that sorts n integers in the range 0 to n5 – 1 in O(n) time. please answer it in 15 min, i'll upvote u
0 0
Add a comment Improve this question Transcribed image text
Answer #1

There are two algorithms whose time complexities are O(n) : Radix Sort and Counting Sort. As the given range on n in the question is quite high, Radix sort will be a better option to implement as it is faster for larger values.

Algorithm for Radix Sort:

  • Step 1 - Define 10 queues each representing a bucket for each digit from 0 to 9.
  • Step 2 - Consider the least significant digit of each number in the list which is to be sorted.
  • Step 3 - Insert each number into their respective queue based on the least significant digit.
  • Step 4 - Group all the numbers from queue 0 to queue 9 in the order they have inserted into their respective queues.
  • Step 5 - Repeat from step 3 based on the next least significant digit.
  • Step 6 - Repeat from step 2 until all the numbers are grouped based on the most significant digit.

Implementation of Radix Sort Algorithm using C Programming Language

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>


int getMax(int list[], int n) {
    int mx = list[0];
    int i;
    for (i = 1; i < n; i++)
        if (list[i] > mx)
            mx = list[i];
    return mx;
}

void countSort(int list[], int n, int exp) {
    int output[n];
    int i, count[10] = { 0 };

    for (i = 0; i < n; i++)
        count[(list[i] / exp) % 10]++;

    for (i = 1; i < 10; i++)
        count[i] += count[i - 1];

    for (i = n - 1; i >= 0; i--) {
        output[count[(list[i] / exp) % 10] - 1] = list[i];
        count[(list[i] / exp) % 10]--;
    }

    for (i = 0; i < n; i++)
        list[i] = output[i];
}

void radixsort(int list[], int n) {
    int m = getMax(list, n);

    int exp;
    for (exp = 1; m / exp > 0; exp *= 10)
        countSort(list, n, exp);
}

void print(int list[], int n) {
    int i;
    for (i = 0; i < n; i++)
        printf("%d\t", list[i]);
}

int main()
{
    int list[] = { 82, 901, 100, 12, 150, 77, 55, 23 };
    int i, n = sizeof(list) / sizeof(list[0]);

    printf("List of numbers before sort: \n");
    for(i = 0; i<8; i++)
        printf("%d\t", list[i] );

    radixsort(list, n);

    printf("\n\nList of numbers after sort: \n");
    print(list, n);
    printf("\n\n");
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
please answer it in 15 min, i'll upvote u Write an algorithm that sorts n integers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Q1. [10 pts] Write an algorithm for Bubble sort that sorts array of n integers. Indicate...

    Q1. [10 pts] Write an algorithm for Bubble sort that sorts array of n integers. Indicate the expected time complexity of the algorithm using the big-O notation. Use the following format for an algorithm pseudocode Function header (.....) Input: Output: Algorithm steps: 1. 2.

  • please, help me! I'll give you an upvote. Thx u 2. Ethylene is produced by the...

    please, help me! I'll give you an upvote. Thx u 2. Ethylene is produced by the dehydrogenation of ethane. If the feed includes 0.65 mol of steam (an inert diluent) per mole of ethane and if the reaction reaches equilibrium at 1200 K and 1 bar, what is the composition of the product gas on a water-free basis?

  • 9. (5 points) Please describe an algorithm that takes as input a list of n integers...

    9. (5 points) Please describe an algorithm that takes as input a list of n integers and finds the number of negative integers in the list. 10. (5 points) Please devise an algorithm that finds all modes. (Recall that a list of integers is nondecreasing if each term of the list is at least as large as the preceding term.) 11. (5 points) Please find the least integer n such that f() is 0(3") for each of these functions f()...

  • Write a program that inputs ten (10) integers and then sorts them in order from largest...

    Write a program that inputs ten (10) integers and then sorts them in order from largest to smallest in the programming language called Perl. You do not need to error check the input; you can assume the user enters integers. You can select the sorting algorithm of your choice, but you must implement this algorithm yourself. You cannot use a built-in sorting function. [15 points] Below is an example of a sample program run: Unsorted: 10, 4, 23, 99, 7,...

  • (V). Given the following algorithm, answer relevant questions. Algorithm 1 An algorithm 1: procedure WHATISTHIS(21,22,...,n: a...

    (V). Given the following algorithm, answer relevant questions. Algorithm 1 An algorithm 1: procedure WHATISTHIS(21,22,...,n: a list of n integers) for i = 2 to n do c= j=i-1 while (j > 0) do if ra; then break end if 4j+1 = a; j= j-1 end while j+1 = 1 end for 14: return 0.02. 1, 15: end procedure Answer the following questions: (1) Run the algorithm with input (41, 02, 03, 04) = (3, 0, 1,6). Record the values...

  • (15 points) Consider the algorithm for insertion sort shown below. The input to this algorithm is...

    (15 points) Consider the algorithm for insertion sort shown below. The input to this algorithm is an earray A. You must assume that indexing begins at 1. 1: for j = 2: A.length do key = A i=j-1 while i > 0 and A[i] > key do Ali + 1] = Ai i=i-1 7: A[i+1] = key (a) Follow this algorithm for A[1..4) =< 7,9,6,8 >. Specifically, please indicate the contents of the array after each iteration of the outer...

  • MATLAB PLEASE MATLAB PLEASE 9. Consider the following discrete-time exponential signal r(n) 75(-0.95)"u(n-3) (a)...

    MATLAB PLEASE MATLAB PLEASE 9. Consider the following discrete-time exponential signal r(n) 75(-0.95)"u(n-3) (a) Plot r(n) over the range (b) Find the sum of the sequence for the above range. Hence, verify the result using the n0,1,2..,20. closed form. (c) Determine the sum of the sequence r(n) over the entire range of integers, i.e. n0,1,2,.o. (d) Find the energy of the sequence x(n) 9. Consider the following discrete-time exponential signal r(n) 75(-0.95)"u(n-3) (a) Plot r(n) over the range (b) Find...

  • 1. Design and write a Divide& Conquer algorithm that, given an array A of n distinct...

    1. Design and write a Divide& Conquer algorithm that, given an array A of n distinct integers which is already sorted into ascending order, will find if there is some i such that Ali] in worst-case 0(log n) time.

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • Below is a linear time complexity algorithm Max-Min-VER1 to find the biggest and smallest element a...

    Below is a linear time complexity algorithm Max-Min-VER1 to find the biggest and smallest element a given list. Input: A is a given list Output: two integers Example: Given A = {1, 5, 9, -3}. It returns -3 (the smallest element), and 9 (the biggest element) Max-Min-VER1(A, n) Line 1: large ← A[0] Line 2: for I from 1 to n - 1 do Line 3:     large ← Math.max(large, A[I]) Line 4: small ← A[0] Line 5: for I from...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT