Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In this bubble sort code what do these variables c & d mean in C?

Tags:

arrays

c

sorting

So I understand that an ascending order bubble sort checks if index i is greater than index "i + 1", and if it is, then it swaps the positions, and then keeps going till it reaches the end of the loop, and then the loop starts over, and keeps switching positions, until every index "i" is not greater than "i + 1", and then you loop is sorted in ascending order completely.

So I'm looking at this bubble sort code. Here is the source: www.programmingsimplified.com/c/source-code/c-program-bubble-sort

Here is the code:

/* Bubble sort code */

#include <stdio.h>

int main()
{
  int array[100], n, c, d, swap;

  printf("Enter number of elements\n");
  scanf("%d", &n);

  printf("Enter %d integers\n", n);

  for (c = 0; c < n; c++)
    scanf("%d", &array[c]);

  for (c = 0 ; c < ( n - 1 ); c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (array[d] > array[d+1]) /* For decreasing order use < */
      {
        swap       = array[d];
        array[d]   = array[d+1];
        array[d+1] = swap;
      }
    }
  }

  printf("Sorted list in ascending order:\n");

  for ( c = 0 ; c < n ; c++ )
     printf("%d\n", array[c]);

  return 0;
}

I understand all variables except "d". I have no clue what they mean esoterically. So this is the section of code that confuses me:

 for (c = 0 ; c < ( n - 1 ); c++)
      {
        for (d = 0 ; d < n - c - 1; d++)
        {
          if (array[d] > array[d+1]) /* For decreasing order use < */
          {
            swap       = array[d];
            array[d]   = array[d+1];
            array[d+1] = swap;
          }
        }
      }

As I understand it, the first for loop steps through the array of length n - 1 because in C the first index of an array is 0. The length of the second for loop appears to be the length of "c" as it steps through the array, but it's not the length of the array (n - 1), so that confuses me. I don't get what d is, so I don't get why "d" is used in the swap statements instead of "c. I totally get what the swap does though, switching the elements of the array as I explained in the intro with swapping the indices.

So I guess what I don't understand the most is why the second for loop is necessary and the length of the array of d:

 for (d = 0 ; d < n - c - 1; d++)
            {
               // if comparison switch check.
            }

So the code provided in the website does a bubble sort correctly. I tried it with n = 5, and values = {9, 8, 7, 6, 5}

It successfully rearranges them to {5, 6, 7, 8 , 9} in ascending order.

Since I don't understand the second for loop, I thought I would see what would happen if I did not include it, and the result is interesting. I just changed removed the second for loop, and replaced mentions of "d" with c.

#include <stdio.h>

int main(void)
{

int array[100], n, c, swap;

printf("Enter number of elements\n");
scanf("%d", &n);

// Tell the user to enter integers in a loop.
printf("Enter %d integers\n", n);
    for (c = 0; c < n; c++)
        scanf("%d", &array[c]);

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

                if(array[c] > array[c + 1])
                {
                    swap = array[c];
                    array[c] = array[c+1];
                    array[c+1] = swap;
                }

        }

        printf("Sorted list in ascending order:\n");

        for(c = 0; c < n; c++)
            printf("%d\n", array[c]);

    return (0);
}

If I see what happens with this code when I removed the second for-loop I get this result. I input 5 elements: {9, 8, 7, 6, 5}, and the order I get back is this: {8, 7, 6, 5, 9}. This is clearly not correct ascending order. But it did a bubble sort through the whole set; it just only completed one pass, so that is why this sorted array starts at 8 because it only made one pass.

Again, this incorrect code now can actually correctly sort this 5 element array: {1, 8, 2, 3, 7}. It changes it to {1, 2, 3, 7, 8}. But it only took one pass through the array to correctly complete this bubble sort.

So I have discovered that the second for loop makes sure the array is numerically sorted in increasing order correctly as many number of passes of bubble sorts are required to make the change.

But I still don't understand it, and it's hard to explain how I don't understand it. I very well know that "c" is the index that slides through the array from beginning to end. But what kind of index is "d" and where does it truly start from, and where does it really end?

Thanks.

If you happen to have simpler code that doesn't require a for-loop inception for a bubble sort, and you can only use one variable, then I would appreciate admiring that code.

like image 379
xyz123 Avatar asked May 03 '17 04:05

xyz123


People also ask

What is bubble sort code in C?

Bubble sort in C is a straightforward sorting algorithm that checks and swaps elements if they are not in the intended order. It compares two adjacent elements to find which one is greater or lesser and switches them based on the given condition until the final place of the element is found.

What does sort do in C?

Introduction to Sorting in C. The process of Sorting can be explained as a technique of rearranging the elements in any particular order, which can be set ready for further processing by the program logic.

What is bubble sort and selection sort in C?

Bubble sort. Selection sort. In bubble sort, two adjacent elements are compared. If the adjacent elements are not at the correct position, swapping would be performed. In selection sort, the minimum element is selected from the array and swap with an element which is at the beginning of the unsorted sub array.

What is I and J in bubble sort?

When i = 0, with the j loop, the largest element of the array reaches its correct position. When i = 1, with the j loop, the second largest element of the array reaches its correct position. So on and so forth.


1 Answers

Here c & d using to traverse and sorting the loop

for c the whole loop is traversing n times

and for d

all elements of the array except the sorted are being traversed and operated for sorting the array

hope that you understand the fact now

Happy Coding

like image 90
SHAH MD IMRAN HOSSAIN Avatar answered Oct 02 '22 16:10

SHAH MD IMRAN HOSSAIN