Jump to content

Using C to develop a bubble sort program


Recommended Posts

I am simply developing bubble sort in C, but I am having trouble collecting array elements from the user; for example, if I enter array size 5, I am unable to save 5 elements in an array. I typed this code within the main method. The following code is taken from scaler.

#include<conio.h>
#include<stdio.h>

void bubbleSort(int *arr, int size) {
          int temp, i, j;
          for(i = 1; i < size; 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;
                              }
                    }
          }
}
void display(int *p, int s) {

          for(int i = 0; i < s; i++){
                    printf("%d, ", *(p+i));
          }
}

int main() {
         int i, size, arr[size];
         printf("\n Enter Array Size ....");
         scanf("%d", &size);
         printf("\n Enter %d array values...", size);
          for(i = 0; i < size; i++){
                    scanf("%d", &arr[i]);
          }
          bubbleSort(arr, size);
          display(arr, size);
          return 0;
}

 

Link to comment
Share on other sites


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.


×
×
  • Create New...