C Menu9>
- Basics of C
- Basics of C Programming
- Features of C Programming
- Preprocessors in C Programming
- History of C Programming
- Low Level Programming
- Why C is Middle Level Language
- Introduction to C programming
- Structure of a C program
- Input-Output Functions
- Difference between Compiler and Interpreter
- Assembler v/s Compiler v/s Interpreter v/s Linker v/s Loader
- Basics to Code
- Operators in C
- Operators in C
- Precedence Associativity Operators
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Ternary Operators in C
- Size of Operators
- Conditional Operators
- Comma Operators
- Format Specifiers in C
- Difference between %d and %i
- Difference between %f, %e, %E and %g
- How to print% using printf
- How to print \ using printf
- How to print “” using printf
- What is the use of %p in C
- Library Functions in C
- pow() in C
- sqrt() in C
- Storage Classes in C
- Conditional statements and Decision Making in C
- DataType & Functions in C
- Arrays, String, Structure, Union and Enum
- Pointer in C
- Library Function in C
- Library function in C
- math.h in c
- Library function math.h acos
- Library function math.h acosh
- Library function math.h asin
- Library function math.h asinh
- Library function math.h atan
- Library function math.h atan2
- Library function math.h atanh
- Library function math.h cbrt
- Library function math.h cell
- Library function math.h cos
- Library function math.h cosh
- Library function math.h exp
- Library function math.h fabs
- Library function math.h floor
- Library function math.h hypot
- Library function math.h log
- Library function math.h log10
- Library function math.h pow
- Library function math.h sin
- Library function math.h sinh
- Library function math.h sqrt
- Library function math.h tan
- Library function math.h tanh
- Library function studio.h clearerr
- Library function string.h strcat
- Library function string.h strcmp
- Library function string.h strcpy
- Library function string.h strlen
- File Handling
- Dynamic Memory Allocation
- Miscellaneous Topics in C
- Miscellaneous Coding Questions
PREPINSTA PRIME
Counting Sort in C
Counting Sort in C
Counting Sort, is an integer sorting algorithm, is a sorting technique in which we sort a collection of elements based on numeric keys between the specific range. In the counting algorithm we don’t compare element while sorting.it is often used as a subroutine in other sorting algorithm. By counting It operates the no. of objects having each distinct key value, after this it perform some arithmetic to calculate the position of each key value in output sequence.
It is used as a subroutine in another sorting algorithm for example radix sort.
If we compare counting sort to bucket sort then we see that bucket sort requires a large amount of preallocated memory or linked list and dynamic array to hold the sets of items within each bucket, whereas counting sort instead stores a single number per bucket.
- Time complexity of counting sort is O((n).
Algorithm of Counting Sort
Counting_Sort(A,B,K)
1-let C[0,k] be a new array
2- for i<-0 to k
3- C[i]<-0
4- for j<-1 to length[A]
5- do C[A[j]] <- C[A[j]]+1
// C[i] now contains the number of elements equal to i.
6- for i<-1 to k
7-do C[i]<- C[i]+C[i-1]
// C[i] now contains the number of elements less than or equal to i.
8- for j<- length[A] down to 1
9-do B[C[A[j]]]<- A[j]
10- C[A[j]]<-C[A[j]]-1.
Execution of Counting Sort
According to algorithm let us take an example
Step 1-
i=0 to 6 since k=6(Largest element in array A)
C[i]<-0
Step 2-
j=1 to 10 since,length[A]=10
C[A[j]]<-C[A[j]]+1
For j=1
C[A[1]]<-C[1]+1= 0+1 =1
C[1]<-1
For j=2
C[A[2]]<-C[6]+1= 0+1 =1
C[6]<-1
similarly for j=5,6,7,8,9,10
Step 3-
For i= 1 to 6
C[i]<-C[i]+C[i-1]
For i=1
C[1]<-C[1]+C[0]
C[1]<-1+0 = 1
For i=2
C[2]<-C[2]+C[1]
C[1]<-1+0 = 1
Similarly for i=4,5,6
Step 4-
For j=10 to 1
B[C[A[j]]]<-A[j]
C[A[j]]<-C[A[j]]-1
B= 1 3 3 3 4 4 5 6 6
Program for Counting Sort in C
// Counting sort in C programming #include<stdio.h> #define MAX 255 void countSort(int array[], int size) { int output[MAX]; int count[MAX]; int max = array[0]; // Here we find the largest item in the array for (int i = 1; i < size; i++) { if (array[i] > max) max = array[i]; } // Initialize the count for each element in array to 0 for (int i = 0; i <= max; ++i) { count[i] = 0; } // For each element we store the count for (int i = 0; i < size; i++) { count[array[i]]++; } // Store the cummulative count of each array for (int i = 1; i <= max; i++) { count[i] += count[i - 1]; } // Search the index of each element of the actual array in count array, and // place the elements in output array for (int i = size - 1; i >= 0; i--) { output[count[array[i]] - 1] = array[i]; count[array[i]]--; } // Transfer the sorted items into actual array for (int i = 0; i < size; i++) { array[i] = output[i]; } } // printing items of the array void display(int array[], int size) { for (int i = 0; i < size; i++) printf("%d ",array[i]); printf("\n"); } // Driver code int main() { int array[] = {2, 5, 2, 8, 1, 4, 1}; int n = sizeof(array) / sizeof(array[0]); countSort(array, n); display(array, n); return 0; }
Output:
1 1 2 2 4 5 8
Sorting
Sorting algorithms are easy to learn but are really important for college semester exams and companies offering package between 3 – 6 LPA would ask direct searching questions in online test/ interviews.