A) What is Huffman Coding? List out a few applications of it. <br> b) Write the algorithm of selection sort and find its time complexity.
Q.) a) What is Huffman Coding? List out a few applications of it.
b) Write the algorithm of selection sort and find its time complexity.
a) Huffman Coding
Huffman coding is a popular method for data compression that is used to minimize the amount of space needed to store a message. It was developed by David A. Huffman in 1952. Huffman coding uses a specific method for choosing the representation for each symbol, resulting in a prefix code (sometimes called "prefix-free codes", i.e., the bit string representing some particular symbol is never a prefix of the bit string representing any other symbol) that expresses the most common source symbols using shorter strings of bits than are used for less common source symbols.
Huffman coding is a two-part process:
- First, a table is built that associates each symbol from the source message with a bit string. This table is known as a Huffman code table.
- The source message is then processed symbol by symbol, and the corresponding bit string for each symbol is output.
Applications of Huffman Coding:
- File Compression: Huffman coding is used in file compression in utilities like WinZip, 7-Zip, etc.
- Image Compression: Huffman coding is used in image compression in formats like JPEG.
- Video Compression: Huffman coding is used in video compression in formats like MPEG.
b) Selection Sort Algorithm
Selection sort is a simple comparison-based sorting algorithm. The main idea behind this algorithm is to divide the input list into two parts: the sorted part and the unsorted part. Initially, the sorted part is empty and the unsorted part is the entire input list. The algorithm repeatedly selects the smallest (or largest, depending on the sorting order) element from the unsorted subarray and moves it to the beginning of the sorted subarray.
Here is the step-by-step algorithm of Selection Sort:
- Find the minimum element in the array and swap it with the element in the first position.
- Find the minimum element in the rest of the array (excluding the first element) and swap it with the element in the second position.
- Repeat the process of finding the minimum element in the rest of the array and swapping it with the element at the beginning of the unsorted part until the entire array is sorted.
Here is a pseudocode representation of the Selection Sort algorithm:
for i from 0 to n-1
min_index = i
for j from i+1 to n
if array[j] < array[min_index]
min_index = j
swap array[min_index] and array[i]
Time Complexity of Selection Sort:
The time complexity of the selection sort algorithm is O(n^2), where n is the number of elements in the array. This is because there are two nested loops in the algorithm: the outer loop runs n times and the inner loop runs n-i times for each iteration of the outer loop, where i is the current iteration number of the outer loop. Therefore, the total number of iterations is n*(n+1)/2, which is in the order of n^2.