- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Sort Elements on the basis of the Number of Factors

In this problem, our task is to sort the array of integers based on several factors of numbers present in the array as a priority. The array is the best way to store similar types of elements in java. But if the number of factors becomes equal in any two numbers, then as a second priority, this algorithm sees numerical value. Factors are the numbers by which the given number is divisible entirely without leaving any remainder. This article uses various approaches to sort elements based on several factors.

## To show you some instances

### Instance-1

If Array = [62, 8, 42, 74, 63]

Then,

Factors(62) = {1, 2, 31, 62} ⇒ 4 Factors(8) = {1, 2, 4, 8} ⇒ 4 Factors(42) = {1, 2, 3, 6, 7, 14, 21, 42} ⇒ 8 Factors(74) = {1, 2, 37, 74} ⇒ 4 Factors(63) = {1, 3, 7, 9, 21, 63} ⇒ 6

Resultant Array after Sorting = [41, 53, 21, 75, 36]

### Instance-2

If Array = [75, 21, 36, 41, 53]

Then,

Factors(75) = {1, 3, 5, 15, 25, 75} ⇒ 6 Factors(21) = {1, 3, 7, 21} ⇒ 4 Factors(36) = {1, 2, 3, 4, 6, 9, 12, 18, 36} ⇒ 9 Factors(41) = {1, 41} ⇒ 2 Factors(53) = {1, 53} ⇒ 2

Resultant Array after Sorting = [41, 53, 21, 75, 36]

### Multiple Approaches

We have provided the solution in different approaches.

By using Brute Force Approach.

By using Divide and Conquer Algorithm.

Let’s start with the program along with its output sequentially.

## Approach-1: By using Brute Force Approach

This algorithm is based on the Brute Force Algorithm. In this sorting, we make a pairwise comparison for N-passes.

### Algorithm: By using Brute Force Approach

**Step-1**: Ask the Number of Elements the user wants to Input in the variable ‘N’.

**Step-2**: Ask ‘N’ elements from the user and store them in an integer array “array”.

**Step-3**: Calculate factors [] array based on the number of factors of the elements in the array[].

**Step-4**: Sort the array [] based on factors[] array as a priority and numerical value as the second priority.

**Step-5**: Display the result in the console.

### Example

#include <iostream> using namespace std; /** * Count the number of factors of the number "num" * * @num: accepting integer number * @return count: return the total number of factors */ int countFactors(int num) { int factorsCount = 0; // traverse i = 1 to num for checking valid factor for (int fact=1; fact <=num; ++fact) { // True, if "fact" is a valid factor of "num" if (num%fact == 0) ++factorsCount; } // return the total number of factors of number "num" return factorsCount; } /** * Swap numbers present in position - pos1 & pos2 in array "arr" * * @param array[] * @param pos1: position of first element in array * @param pos2: position of second element in array */ void swap(int array[], int pos1, int pos2) { int tempVar = array[pos1]; array[pos1] = array[pos2]; array[pos2] = tempVar; } /** * Sorting array based on number of factors * * @param arr[]: array for sorting * @param n: number of elements present in array "arr" */ void sort(int arr[], int n) { // create counter array for storing the number of factors // this is used for mapping the number with its factor-counts int counter[n]; for (int idx=0; idx <n; ++idx) counter[idx] = countFactors(arr[idx]); // sort the "array" using the concept of "bubble sort" for (int pass=0; pass <n; ++pass) { for (int idx=0; idx <n-pass-1; ++idx) { // if order is not correct, swap the elements if ((counter[idx] > counter[idx+1]) || (counter[idx] == counter[idx+1] && arr[idx] > arr[idx+1])) { swap(arr, idx, idx+1); swap(counter, idx, idx+1); } } } } /** * main() function */ int main() { int N, array[100000]; // ask for the "number" of elements the user wants to enter cout < < "Enter N: "; cin >> N; // ask N elements from the user cout < < "Enter " < < N < < " Elements: "; for (int idx=0; idx<N; ++idx) { cin >> array[idx]; } // sort the array using function sort() sort(array, N); // display the array after sorting cout < < "Array after Sorting by Factor: "; for (int idx=0; idx <N; ++idx) { cout < < array[idx] < < " "; } cout < < endl; // end of program return 0; }

### Output

Enter N: 5 Enter 5 Elements: 75 21 36 41 53 Array after Sorting by Factor: 41 53 21 75 36

**Time Complexity of Program** = O(N*N)

**Space Complexity of Program** = O(N)

## Approach-2: By using Divide and Conquer Algorithm

This algorithm is based on the divide and conquers technique. In this, the array[] and counter[] are divided recursively in the divide phase, and then, in conquer phase, both the sorted parts are merged in a combined sorted form.

### Algorithm: By using Divide & Conquer Algorithm

**Step-1**: Take user input for the variable ‘N’.

**Step-2**: Ask ‘N’ elements from the user and store them in an integer array “array”.

**Step-3**: Compute factors [] array depends on the number of factors of the elements in the array[].

**Step-4**: Recursively divide the array into two halves till the size of the array remains 1.

**Step-5**: After returning from recursion, merge the two halves of the array in sorted form.

**Step-6**: Display the sorted array in the console.

### Example

#include <iostream> using namespace std; /** * count the number of factors of number "num" * * @num: accepting integer number * @return count: return total number of factors */ int countFactors(int num) { int factorsCount = 0; // traverse i = 1 to num for checking valid factor for (int fact=1; fact<=num; ++fact) { // True, if "fact" is a valid factor of "num" if (num%fact == 0) ++factorsCount; } // return the total number of factors of number "num" return factorsCount; } /** * Merge two sub-arrays, arr[l: m+1] and arr[m+1: r] * based on counter[] array */ void merge(int arr[], int counter[], int l, int m, int r) { int temp1[r-l+1], temp2[r-l+1]; int i = l, j = m+1, k = 0; while (i<=m && j<=r) { if ((counter[i] < counter[j]) || (counter[i] == counter[j] && arr[i]<=arr[j])) { temp1[k] = arr[i]; temp2[k] = counter[i]; i += 1; } else { temp1[k] = arr[j]; temp2[k] = counter[j]; j += 1; } k += 1; } while (i<=m) { temp1[k] = arr[i]; temp2[k] = counter[i]; k += 1; i += 1; } while (j<=r) { temp1[k] = arr[j]; temp2[k] = counter[j]; k += 1; j += 1; } i = 0, j = l; while (j <= r) { arr[j] = temp1[i]; counter[j] = temp2[i]; i += 1; j += 1; } } // recursive algorithm void sort(int arr[], int counter[], int l, int r) { if (l >= r) return; int m = (l+r)/2; sort(arr, counter, l, m); sort(arr, counter, m+1, r); merge(arr, counter, l, m, r); } /** * main() function */ int main() { int N, array[100000], counter[100000]; // ask for "number" of elements user want to enter cout << "Enter N: "; cin >> N; // ask N elements from the user cout << "Enter " << N << " Elements: "; for (int idx=0; idx<N; ++idx) { cin >> array[idx]; } // create counter array for storing the number of factors // this is used for mapping the number with its factor-counts for (int idx=0; idx<N; ++idx) counter[idx] = countFactors(array[idx]); // sort the array using function sort() sort(array, counter, 0,N-1); // display the array after sorting cout << "Array after Sorting by Factor: "; for (int idx=0; idx<N; ++idx) { cout << array[idx] << " "; } cout << endl; // end of the program return 0; }

### Output

Enter N: 5 Enter 5 Elements: 62 8 42 74 63 Array after Sorting by Factor: 8 62 74 63 42

**Time Complexity of Program** = O(N*log(N))

**Space Complexity of Program** = O(N)

In this article, we give two approaches based on Brute-Force and Divide and Conquer techniques. Among them, Divide and Conquer Technique performed well.