Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting array with many equal values java [duplicate]

if I am having an array where nearly 90% of its values are equal and i want to sort it. what would be the best sorting method should I use ?

like image 749
A.khaled Avatar asked May 12 '18 16:05

A.khaled


People also ask

How will you sort an array with many duplicated values in Java?

A simple solution would be to use efficient sorting algorithms like Merge Sort, Quicksort, Heapsort, etc., that can solve this problem in O(n. log(n)) time, but those will not take advantage of the fact that there are many duplicated values in the array. A better approach is to use a counting sort.

How do you find duplicates in an array if there is more than one duplicate?

Duplicate elements can be found using two loops. The outer loop will iterate through the array from 0 to length of the array. The outer loop will select an element. The inner loop will be used to compare the selected element with the rest of the elements of the array.

Which sorting algorithm is best for identical elements?

3-way QuickSort performs very well when there are large number of duplicates. Save this answer.


1 Answers

If you have many repetitions in the array, then you can use a different approach to sort it. For this IMO, self balancing binary tree like AVL or Red black tree would be better approach than any other sorting algorithms.

The idea is to extend tree node to have count of keys also.

class Node {
   int key;
   Node left, right;
   int count;  // Added to handle duplicates
   // Other tree node info for balancing like height in AVL
}

Below is complete algorithm using AVL tree.

1) Create an empty AVL Tree with count as an additional field.

2) Traverse input array and do following for every element ‘arr[i]’

   a) If arr[i] is not present in tree, then insert it and initialize count as 1

   b) Else increment its count in tree.

3) Do Inorder Traversal of tree. While doing inorder put every key its count times in arr[].

The 2nd step takes O(n Log m) time and 3rd step takes O(n) time. So overall time complexity is O(n Log m), where m is number of distinct elements.

The detailed explanation and implementation are given here: Geeks for Geeks

like image 80
Kaushal28 Avatar answered Oct 25 '22 12:10

Kaushal28