![]() |
Given an array of Numbers that contain any frequency of exactly three unique elements. Our task is to sort this array using JavaScript. Examples: Input: arr[] = [3, 1, 3, 2, 1, 2, 1, 3, 2] Table of Content Counting Sort with Extra SpaceIn this approach, we use counting sort to sort the array. We count the occurrences of each unique element and then construct the sorted array based on the counts. Example: Implementation of a program to sort an array of exactly three unique repeating elements using Counting Sort with Extra Space
Output Sorted array: [ 1, 1, 1, 2, 2, 2, 3, 3, 3 ] Time Complexity: O(n) Auxiliary Space: O(n) Three-Way PartitioningIn this approach we use three-way partitioning technique to sort the array in place. Here we maintain three pointers (low, mid, and high) to partition the array into three sections and thus sortiing them. Example: Implementation of program to sort array of exactly three unique repeating elements using Three-Way Partitioning
Output Sorted array: [ 1, 1, 1, 2, 2, 2, 3, 3, 3 ] Time Complexity: O(N),where n is the length of the input array Auxiliary Space: O(1) |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 18 |