Merge sort program using c language

https://www.programiz.com/online-compiler/60moA4PIpU4GL

 #include <stdio.h>

// Function to display the array (can show full array or subpart)

void displayArray(int a[], int start, int end) {

    for (int i = start; i < end; i++)

        printf("%d ", a[i]);

    printf(" ");

}

// Selection Sort Function

void selectionSort(int a[], int n) {

    for (int i = 0; i < n - 1; i++) {

        int min = i;

        // Find the minimum element in remaining unsorted array

        for (int j = i + 1; j < n; j++) {

            if (a[j] < a[min])

                min = j;

        }

        // Swap found minimum element with the first element

        if (min != i) {

            int temp = a[min];

            a[min] = a[i];

            a[i] = temp;

        }

    }

}


int main() {

    int a[] = {12, 5, 10, 9, 7, 6};

    int n = sizeof(a) / sizeof(a[0]);


    printf("Original Array:");

    displayArray(a, 0, n); // Displays whole array before sorting


    printf("\n 1st Subpart of Array (first 3 elements):");

    displayArray(a, 0, 3); // Displays subpart of array

printf("\n 2nd Subpart of Array (last 3 elements):");

    displayArray(a, 3, n);

    // Sort the array

    selectionSort(a, n);


    printf("\n Sorted Array:");

    displayArray(a, 0, n); // Displays whole array after sorting


    return 0;

}

Comments

Popular posts from this blog

B.tech 5th Sem Machin learning lab practical

BCA 3rd year Web Technology(code 304) Web Site development using ASP.NET AND C#

DWDM 2024 question paper with solution