C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Dynamic Memory Allocation in C

Dynamic sorting of array

C Program: Dynamic sorting of array

C

#include <stdio.h>

#include <stdlib.h>

 

int main() {

    int *arr;

    int size, i, j, temp, addCount;

    char choice;

 

    // Step 1: Allocate memory dynamically

    printf("Enter initial number of elements: ");

    scanf("%d", &size);

 

    arr = (int *)malloc(size * sizeof(int));

    if (arr == NULL) {

        printf("Memory allocation failed!\n");

        return 1;

    }

 

    // Step 2: Input array elements

    printf("\nEnter %d elements:\n", size);

    for (i = 0; i < size; i++) {

        printf("Element %d: ", i + 1);

        scanf("%d", &arr[i]);

    }

 

    // Step 3: Option to add more elements dynamically

    printf("\nDo you want to add more elements? (y/n): ");

    scanf(" %c", &choice);

 

    while (choice == 'y' || choice == 'Y') {

        printf("How many elements do you want to add? ");

        scanf("%d", &addCount);

 

        arr = (int *)realloc(arr, (size + addCount) * sizeof(int));

        if (arr == NULL) {

            printf("Memory reallocation failed!\n");

            return 1;

        }

 

        printf("\nEnter %d new elements:\n", addCount);

        for (i = size; i < size + addCount; i++) {

            printf("Element %d: ", i + 1);

            scanf("%d", &arr[i]);

        }

 

        size += addCount;

 

        printf("\nDo you want to add more elements? (y/n): ");

        scanf(" %c", &choice);

    }

 

    // Step 4: Display unsorted array

    printf("\nUnsorted Array:\n");

    for (i = 0; i < size; i++) {

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

    }

    printf("\n");

 

    // Step 5: Sort array using Bubble Sort

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

        for (j = 0; j < size - i - 1; j++) {

            if (arr[j] > arr[j + 1]) {

                temp = arr[j];

                arr[j] = arr[j + 1];

                arr[j + 1] = temp;

            }

        }

    }

 

    // Step 6: Display sorted array

    printf("\nSorted Array (Ascending Order):\n");

    for (i = 0; i < size; i++) {

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

    }

    printf("\n");

 

    // Step 7: Free allocated memory

    free(arr);

    printf("\nMemory freed successfully.\n");

 

    return 0;

}

Output

 
OUTPUT :
Enter initial number of elements: 3
Enter 3 elements:
Element 1: 40
Element 2: 10
Element 3: 25

Do you want to add more elements? (y/n): y
How many elements do you want to add? 2

Enter 2 new elements:
Element 4: 5
Element 5: 60

Do you want to add more elements? (y/n): n

Unsorted Array:
40 10 25 5 60

Sorted Array (Ascending Order):
5 10 25 40 60

Memory freed successfully.


Explanation

Step

Description

1

Allocate initial memory using malloc().

2

Input initial array elements.

3

Ask user if they want to add more elements, use realloc() to resize dynamically.

4

Display the unsorted array.

5

Sort array using Bubble Sort.

6

Display sorted array in ascending order.

7

Free memory using free().