Question

[Code in C] Help me with this. Not sure what to do.

1 Couting Sort You may have learned some sorting algorithms - such as bubble sort ad quicksort in CS 110 and CS 210. This homework is about counting sort. Let n be the number of elements to be sorted. Bubble sort and quicksort assume tha time, which one is larger and which one is smaller. They make no assumption on the values of the elements t we can compare any pair of elements and find out, in constant Counting sort assumes the elements are integers between 0 and m, and m is much smaller than n. For example, let m be 3, and n be 10. Then the elements can be 0, 1. or 2, and we have ten of them. unsigned data[10]-0, 2, 1, 1, 0, 2, 2, 0, 1, 1}; unsigned count [3]; Counting sort uses an array unsigned count [m] and initializes all elements in count to zero. Then we scan the array data. When we see a number data[i], this number is used as the index to the array count, and the corresponding count is incremented. After we finish scanning the array data, we have count [0]-3. count [1]-4, and count [2] == 3. How do we construct the sorted array from these counts? We write 0 for count [0] times, 1 for count [1] times, and 2 for count [2] mes Bubble sort takes O(n2) time in the worst case, and quicksort takes O(nlgn) time on average Loosely speaking, quicksort is faster than bubble sort. You will learn the big-O notation in CS 310 Counting sort takes O(m+n) time. We say it is a linear time algorithm, which is, loosely speaking, faster than quicksort The code in main.c is the driver. You implement counting sort in cntSort.c. The driver runs your counting sort as well as qsort in the standard library. It verifies that your code works correctly, and reports the runtime of both sorting methods.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

#include <stdlib.h>

void cntSort ( unsigned m , unsigned n, unsigned data[]){

unsigned *count = (unsigned *)malloc(m*sizeof(unsigned));

for(int i=0 ; i<n; ++i){

count[data[i]]++;

}

int p = 0;

for(int i=0 ; i<m; ++i){

for(int j=0 ; j<count[i]; ++j){

data[p++] = i;

}

}

free(count);

}

int main() {

unsigned data[10] = { 0,2,1,1,0,2,2,0,1,1};

int m = 3;

printf("UnSorted before Counting Sort: \n");

for(int i=0 ; i<10; ++i)

printf("%d ", data[i]);

cntSort(3, 10,data);

printf("\n\nSorted after Counting Sort: \n");

for(int i=0 ; i<10; ++i)

printf("%d ", data[i]);

return 0;

}

=========================
SEE OUTPUT

main.c E saved #include <stdio.h> #include <stdlib.h> void cntSort ( unsigned m, unsigned n, unsigned data[]) { unsigned *cou



Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
[Code in C] Help me with this. Not sure what to do. 1 Couting Sort You...
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
  • C++ Search & Sort

    Exercise #2: Design and implement a program (name it SimpleSort) to implement and test the three sort algorithms (Bubble, Insertion, Selection) discussed in the lecture slides.  Define method BubbleSort() to implement Bubble sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array.  Define method InsertionSort() to implement insertion sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define...

  • 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...

  • Implement quicksort and bucket sort. Use the code in your book to help; the partition in...

    Implement quicksort and bucket sort. Use the code in your book to help; the partition in quicksort is tricky. Make sure your implementations are correct — it is easy to gain some confidence in the correctness of your code by writing a program which creates arrays filled with random numbers, sorts them, and then checks that they are sorted. Then time your code (using clock() or similar methods) on both methods for arrays filled with random integers of the following...

  • Java Question: Code the Merge Sort in such a way that it outputs the number of...

    Java Question: Code the Merge Sort in such a way that it outputs the number of comparisons and the number of swaps performed when sorting a random set of items. Then use it to sort 1000, 5000, 10,000, and 100,000 integers. Tabulate the results and compare it to the number of comparisons and swaps calculated using the formulas given in Table 8.6. Table 8.6 Performance of the Quicksort Algorithm Speed Memory Overhead Range Bytes lgorithm Range Effort Comments Binary Tree...

  • The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also...

    The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also please explain why the seg fault is happening. Thank you #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size;...

  • Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template...

    Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template of the solution, if you need Write a program that asks users to input 10 integers into an array, write a function that takes the array as its argument, use bubble sort to sort the array and output the sorted array in increasing order. #include <iostream> using namespace std; C:IWINDOWSSystems2cmd.exe lease input 10 integers: void bubblesort(int a[10]) int help; int b[10]; for (int i...

  • sample bubble sort code: ;---------------------------------------------------------- BubbleSort PROC USES eax ecx esi, pArray:PTR DWORD, ; pointer to...

    sample bubble sort code: ;---------------------------------------------------------- BubbleSort PROC USES eax ecx esi, pArray:PTR DWORD, ; pointer to array Count:DWORD ; array size ; ; Sort an array of 32-bit signed integers in ascending ; order, using the bubble sort algorithm. ; Receives: pointer to array, array size ; Returns: nothing ;----------------------------------------------------------- mov ecx,Count dec ecx ; decrement count by 1 L1: push ecx ; save outer loop count mov esi,pArray ; point to first value L2: mov eax,[esi] ; get array...

  • In C++ language, implement a class that can sort an array of numbers using all three...

    In C++ language, implement a class that can sort an array of numbers using all three algorithms we have seen in this course, but each method updates a “counter” value every time it accesses the array. Have it print this at the end of the sorting process. Store the array values in an “original” array so you don’t have to re-type it for different sorts (since each sort alters the array), and have the sort modify a copy. Note: IF...

  • In C++ language, implement a class that can sort an array of numbers using all three...

    In C++ language, implement a class that can sort an array of numbers using all three algorithms we have seen in this course, but each method updates a “counter” value every time it accesses the array. Have it print this at the end of the sorting process. Store the array values in an “original” array so you don’t have to re-type it for different sorts (since each sort alters the array), and have the sort modify a copy. Note: IF...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

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
ADVERTISEMENT