C Program to Implement Bubble Sort Using Pointers

Introduction

Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. In C, you can implement Bubble Sort using pointers to directly manipulate the elements of the array. This guide will show you how to write a C program to implement Bubble Sort using pointers.

Example:

  • Input: Array [5, 2, 9, 1, 5, 6]
  • Output: Sorted array [1, 2, 5, 5, 6, 9]

Problem Statement

Create a C program that:

  • Takes an array of integers as input from the user.
  • Uses pointers to implement the Bubble Sort algorithm to sort the array.
  • Displays the sorted array.

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> for standard input-output functions.
  2. Declare the Array and Pointer Variables: Declare an integer array to store the elements and pointers for accessing and manipulating the elements.
  3. Input the Array Elements: Use a loop to take input for the array elements from the user.
  4. Implement Bubble Sort Using Pointers: Use pointers to traverse the array, compare adjacent elements, and swap them if needed.
  5. Display the Sorted Array: Use printf to display the sorted array.

C Program to Implement Bubble Sort Using Pointers

#include <stdio.h>

int main() {
    // Step 2: Declare the array and pointer variables
    int arr[100], n;
    int *ptr1, *ptr2, temp;

    // Step 3: Input the number of elements and the array elements
    printf("Enter the number of elements: ");
    scanf("%d", &n);

    printf("Enter the elements of the array: ");
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // Step 4: Implement Bubble Sort using pointers
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            ptr1 = &arr[j];
            ptr2 = &arr[j + 1];

            if (*ptr1 > *ptr2) {  // Compare the elements pointed by ptr1 and ptr2
                // Swap the elements
                temp = *ptr1;
                *ptr1 = *ptr2;
                *ptr2 = temp;
            }
        }
    }

    // Step 5: Display the sorted array
    printf("Sorted array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;  // Return 0 to indicate successful execution
}

Explanation

Step 2: Declare the Array and Pointer Variables

  • The integer array arr is declared to store the elements of the array.
  • The pointers ptr1 and ptr2 are used to traverse and compare elements in the array.
  • The variable temp is used for swapping elements during the sorting process.

Step 3: Input the Array Elements

  • The program prompts the user to enter the number of elements in the array and stores this value in n.
  • A for loop is used to take input for each element of the array.

Step 4: Implement Bubble Sort Using Pointers

  • The program uses a nested for loop to implement the Bubble Sort algorithm:
    • The outer loop runs n-1 times, where n is the number of elements in the array.
    • The inner loop compares adjacent elements using pointers ptr1 and ptr2.
    • If the element pointed to by ptr1 is greater than the element pointed to by ptr2, the elements are swapped using the temp variable.
  • This process continues until the entire array is sorted in ascending order.

Step 5: Display the Sorted Array

  • The program uses a for loop to display the sorted array.

Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example Output:

Enter the number of elements: 6
Enter the elements of the array: 5 2 9 1 5 6
Sorted array: 1 2 5 5 6 9

Conclusion

This C program demonstrates how to implement Bubble Sort using pointers. It covers basic concepts such as pointer manipulation, array traversal, and sorting algorithms, making it a useful example for beginners learning C programming.

Leave a Comment

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

Scroll to Top