Arrange the Array such that upon Performing given Operations an Increasing order is Obtained


You must use the proper sorting algorithms in order to organise an array in increasing order using the specified operations. Determine the most effective method first depending on the array size and data properties. Bubble Sort, Merge Sort, and Quick Sort are examples of popular sorting algorithms. Apply the chosen algorithm repeatedly, shifting the positions of elements based on comparisons between them until the array is organised in ascending order. The effectiveness of an algorithm is determined by how time−consuming it is, with the best ones producing quicker results. The array may be effectively organised in increasing order by carefully using the selected sorting method, making it easier to manipulate and analyse the data.

Methods Used

  • Bubble Sort

  • Merge Sort

  • Quick Sort

Bubble Sort

Bubble Sort is a straightforward sorting technique that may be used to arrange an array in ascending order. It operates by repeatedly comparing nearby array elements to see whether they are in the correct order, and if not, swapping them. The procedure is repeated until the entire array has been sorted. You can use this sorting method to quickly arrange the array in ascending order. But compared to alternative sorting algorithms like Merge Sort or Quick Sort, which have superior time complexities for larger datasets, Bubble Sort is less effective for larger arrays since its worst−case time complexity is O(n2).

Algorithm

  • Start with an array of unsorted elements.

  • The first and second elements should be compared. Swap the two elements if the first is greater than the second.

  • Repeat the comparison and swapping process for the following pair of adjacent elements, if necessary.

  • Up until the end of the array, repeat this procedure for each pair of neighbouring elements.

  • The largest member in the array will eventually "bubble up" to the final place after being traversed once.

  • Until the entire array is arranged in ascending order, repeat steps 2 through 5 for each of the remaining array elements (apart from the final element sorted).

  • The array is now in increasing order, signalling the end of the sorting operation.

Example

#include <iostream>

template <typename T, size_t N>
void bubbleSort(T (&arr)[N]) {
    for (size_t i = 0; i < N - 1; ++i) {
        for (size_t j = 0; j < N - i - 1; ++j) {
            if (arr[j] > arr[j + 1]) {
                std::swap(arr[j], arr[j + 1]);
            }
        }
    }
}

int main() {
    int arr[] = {64, 34, 25, 12, 22, 11, 90};
    bubbleSort(arr);
    for (int num : arr) {
        std::cout << num << " ";
    }
    return 0;
}

Output

11 12 22 25 34 64 90 

Merge Sort

The divide−and−conquer tactic is used by Merge Sort to arrange an array in increasing order. It splits the array in half and then recursively sorts each sub−array separately. The sorted sub−arrays are then merged back together, ensuring that the elements are in ascending order. The array gets sorted completely once this operation is finished. Merge Sort is an effective method for achieving an increasing order in arrays since it ensures a time complexity of O(n log n) for all instances, making data management and analysis simpler.

Algorithm

  • The array is already sorted if it only contains one element or is empty. Give the array back in its original form.

  • Make two equal parts of the array.

  • Apply Merge Sort iteratively to each half to independently sort them.

  • Reconstruct the two sorted halves, making that the components are in ascending order.

    Comparing the elements in the two halves, the smaller element is added to a temporary combined array.

    Continue on to the following element in the sub−array where the smaller element was chosen.

    Up till the two parts are combined, compare and merge again.

  • Replace the original array with the combined array.

  • Continue the recursion after sorting the entire array.

  • The merge sort technique has been used to sort the array successfully and in ascending order.

Example

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

void mergeSort(int arr[], int left, int right);
void merge(int arr[], int left, int mid, int right);

int main() {
    const int size = 10;
    int arr[size];

    srand(time(0));
    cout << "Original Array: ";
    for (int i = 0; i < size; i++) {
        arr[i] = rand() % 100;
        cout << arr[i] << " ";
    }

    mergeSort(arr, 0, size - 1);

    cout << "\nSorted Array: ";
    for (int i = 0; i < size; i++) {
        cout << arr[i] << " ";
    }

    return 0;
}

void mergeSort(int arr[], int left, int right) {
}
void merge(int arr[], int left, int mid, int right) {
}

Output

Original Array: 64 10 16 73 95 69 25 68 11 45 
Sorted Array: 64 10 16 73 95 69 25 68 11 45 

Quick Sort

An array is split into smaller and larger sub−arrays by choosing a "pivot" element from the array, according to the common sorting technique known as Quick Sort. The method then sorts these sub−arrays iteratively. The array gets sorted completely once this operation is finished. Quick Sort is effective for big arrays due to its average time complexity of O(n log n). The array can be rearranged in ascending order by carefully using Quick Sort, making data processing and analysis easier.

Algorithm

  • Choose an important element from the list; this element might be any one, but is usually the first, last, or middle member.

  • Rearrange the items of the array to create a partition, putting smaller elements on the left and larger ones on the right.

  • Apply Quick Sort in a recursive manner to both the left and right partitions (items less than and greater than the pivot, respectively).

  • Repeat the procedure for every partition up until the sub−arrays contain nothing or just one element.

  • The sub−arrays are joined as the recursion unwinds to produce the final sorted array.

Example

#include <iostream>
#include <vector>

int partition(std::vector<int>& arr, int low, int high) {
    int pivot = arr[high];
    int i = low - 1;

    for (int j = low; j < high; j++) {
        if (arr[j] < pivot) {
            i++;
            std::swap(arr[i], arr[j]);
        }
    }

    std::swap(arr[i + 1], arr[high]);
    return i + 1;
}

void quickSort(std::vector<int>& arr, int low, int high) {
    if (low < high) {
        int pivotIndex = partition(arr, low, high);
        quickSort(arr, low, pivotIndex - 1);
        quickSort(arr, pivotIndex + 1, high);
    }
}

int main() {
    std::vector<int> arr = {38, 27, 43, 3, 9, 82, 10};
    int n = arr.size();

    quickSort(arr, 0, n - 1);

    std::cout << "Sorted array: ";
    for (int num : arr) {
        std::cout << num << " ";
    }
    return 0;
}

Output

Sorted array: 3 9 10 27 38 43 82 

Conclusion

In conclusion, it is clear that each approach has advantages and disadvantages after taking into account the three sorting algorithms (Bubble Sort, Merge Sort, and Quick Sort) to organise an array in increasing order. Even though it is simple to construct, bubble sort loses effectiveness for larger arrays due to its worst−case O(n2) time complexity. Merge Sort is appropriate for larger datasets and offers consistent performance in all situations thanks to its O(n log n) time complexity. Quick Sort is effective for its efficiency and performs well in the majority of situations. It also has an average time complexity of O(n log n). The exact requirements and qualities of the data being sorted determine the sorting algorithm to be used.

Updated on: 02-Aug-2023

64 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements