This blog is under construction

Sunday, 21 July 2013

C program to perform bubble sort using recursion

Write a C program to perform bubble sort using recursion.


  #include <stdio.h>
  #include <stdlib.h>

  /* sorts the given numbers in ascending order */
  void bubbleSort(int *data, int n) {
        int i, temp;

        if (n > 0) {
                for (i = 1; i < n; i++) {
                        if (data[i - 1] > data[i]) {
                                temp = data[i];
                                data[i] = data[i - 1];
                                data[i - 1] = temp;
                        }
                }

                bubbleSort(data, n - 1);
        }

        return;
  }

  int main() {
        int i, n, *data;

        /* get the number of inputs from the user */
        printf("Enter the number of inputs:");
        scanf("%d", &n);

        /* dynamically allocate memory to store i/p values */
        data = (int *) malloc(sizeof(int) * n);

        /* get the input data from the user */
        for (i = 0; i < n; i++) {
                printf("data[%d]: ", i);
                scanf("%d", &data[i]);
        }

        /* sorts the given numbers */
        bubbleSort(data, n);

        /* print the sorted numbers */
        printf("Data After Bubble Sort:\n");
        for (i = 0; i < n; i++) {
                printf("%d ", data[i]);
        }
        printf("\n");

        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the number of inputs:5
  data[0]: 500
  data[1]: 100
  data[2]: 300
  data[3]: 200
  data[4]: 400

  Data After Bubble Sort:
  100 200 300 400 500 


2 comments:

  1. i wrote bubblesort program without using pointers can i submit it.....

    ReplyDelete
  2. This is real good! This blog rocks. I knew bubble sort, but I didn't know how to use recursion for the same.

    ReplyDelete