Sorting Operations On Array In Data Structure

Sorting an array is a common operation in data structures and is often used to organize data in a specific order. There are several sorting algorithms that can be used to sort an array, each with different time and space complexities.

One of the most commonly used sorting algorithms is the Quicksort algorithm, which has an average time complexity of O(nlogn) and a worst-case time complexity of
O(n^2). Quicksort works by partitioning an array into two sub-arrays based on a pivot element, and then recursively sorting the sub-arrays.

Another popular sorting algorithm is the Merge Sort algorithm, which has a time complexity of O(nlogn) in both the average and worst-case scenarios. Merge Sort works by dividing the array into two halves, recursively sorting the halves, and then merging the sorted halves back together.

Insertion Sort is another simple sorting algorithm that can be used to sort an array in place. It has an average and worst-case time complexity of O(n^2). Insertion Sort works by iteratively inserting each element of the array into its proper position in a sorted sub-array.

There are also other sorting algorithms such as Bubble Sort, Selection Sort, and Heap Sort, each with its own advantages and disadvantages.

When choosing a sorting algorithm, it's important to consider the size of the array, the desired order of the sorted array, and the available memory. Some sorting algorithms may be more efficient for small arrays, while others may be more efficient for large arrays. Similarly, some sorting algorithms may be more efficient for sorting data in a specific order, such as ascending or descending order.


Bubble sort program-
#include <stdio.h>

void bubble_sort(int arr[], int n) {
int i, j;
    for (i = 0; i < n-1; i++) {
    // Last i elements are already sorted
        for (j = 0; j < n-i-1; j++) {
        // Swap if the element found is greater than the next element
            if (arr[j] > arr[j+1]) {
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
}

int main() {
    int arr[] = {64, 34, 25, 12, 22, 11, 90};
    int n = sizeof(arr)/sizeof(arr[0]);
    bubble_sort(arr, n);
    printf("Sorted array is: ");
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
    return 0;
}

In this implementation, the bubble_sort() function takes an unsorted array and its length as input and modifies the array in place to produce a sorted array. The outer loop iterates through the array from the first element to the last element, while the inner loop iterates through the unsorted elements of the array.

During each iteration of the inner loop, if the current element is greater than the next element, they are swapped. This causes the largest unsorted element to "bubble up" to the end of the array during each iteration. After each pass through the inner loop, the last element is guaranteed to be sorted, so the outer loop can start one element earlier.

The sorted array is printed to the console at the end of the program using a for loop.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext