How to merge unsorted arrays and sort them in C++

Hello learners, In this tutorial, we will deal with the merging and sorting concepts of C++. This topic is very interesting and is very useful in the IT industry. So, without lagging the concept let us understand the concept of merging and sorting arrays in C++.

There are many ways to convert an unsorted array into the sorted one. let us discuss the types of sorting techniques in a programming language.

Types of Sorting techniques:

  • Bubble Sort
  • Selection Sort
  • Insertion Sort
  • Quick Sort
  • Merge Sort
  • Shell Sort
  • Heap Sort

Merge unsorted arrays and sort after merging the arrays in C++

  1. Initially, we include all required headerfiles into the program and then we create a function (say, sort()). The function can be created before the main() function or after the main() function. But, if we want to define the function after the main() then, we have to declare it in the beginning and at last, it can be defined. In the sort() function, we will write the logic behind the sorting of an unsorted array that doesn’t return any value. The sorting technique used here is the Selection sort as we select the minimum element by iterating through every element in the array and placing it at its position. This should be looped till we reach the last element of the array and hence we use nested looping in the sort() function of the program.
  2. Then, we write the main() function from which the execution of the program starts. In the main() function, we implicitly, create two integer type of arrays and initialize them with some random numbers. we declared a variable len in which we will store the sum of all elements in both the arrays then we declare another array of size len to store the merged elements.
  3. In the first for loop, we insert all the elements of arr1 to arr3. The same is done for arr2 using the second for loop. The variable ‘k’ is to store the current index value of arr3 after inserting the arr1 elements into arr3. Then, we print the merged array and call the sort() function in which the elements will get sorted. Lastly, we print all the elements of the sorted array arr3.

CODE IN C++:

#include<bits/stdc++.h>
#include <iostream>
using namespace std;
void sort(int arr[], int length) {
    int i,j,min;
    for (i=0;i<length-1; i++) { 
        min = i;
        for(j=i+1;j<length;j++) 
            if (arr[j]<arr[min]) { 
                min = j;
            }
        int temp = arr[min]; 
        arr[min] = arr[i]; 
        arr[i] = temp; 
    }
}
int main() {
    int arr1[] = {3, 2,3,8,1};
    int arr2[] = {65, 8, 9,34,56};
    int i,j, k=0;
    int len = (sizeof(arr1)/sizeof(arr1[0])) + (sizeof(arr2)/sizeof(arr2[0]));
    int arr3[len];
    for(i=0,j=0;i<sizeof(arr1)/sizeof(arr1[0]);i++,j++) {
        arr3[j] = arr1[i];
    }
    k = j;
    for(i=0, j=k;i<sizeof(arr2)/sizeof(arr2[0]);i++,j++) {
        arr3[j] = arr2[i];
    }
    cout<<"The merged array is: "<<endl;
    for(i=0;i<len;i++) {
        cout<<arr3[i]<<" ";
    }
    sort(arr3, len);
    cout << "\nThe sorted array is : ";
    for(i=0;i<len;i++) {
        cout<<arr3[i]<<" ";
    }
}

OUTPUT:

The merged array is:
3 2 3 8 1 65 8 9 34 56
The sorted array is : 1 2 3 3 8 8 9 34 56 65

Also read: Conversion of character array to a string in C++

Leave a Reply

Your email address will not be published. Required fields are marked *