Taking an Element From an Array Then Checking to Make Sure It Hasnt Been Chosen Again Python

C Plan to sort an array in ascending order using Bubble Sort

Last updated on September 23, 2020


Bubble Sort #

Chimera sort is a simple method that sorts the elements of an array into either increasing or decreasing order. It works by comparing the adjacent elements and swapping them if they are out of order. Multiple passes through the array are necessary.

The following are the steps to sort an array of size N in ascending order using bubble sort:

Passthrough #1:

  1. Compare arr[0] with arr[one]. If arr[0] > arr[ane], bandy them.
  2. Compare arr[1] with arr[2]. If arr[1] > arr[two], swap them.
  3. Finally, compare a[N-2] with arr[N-1], if arr[N-2] > arr[N-i], swap them.

This completes the first passthrough.

After, the first passthrough, the highest value in the array will be at the terminate.

Passthrough #2:

  1. Compare arr[0] with arr[one]. If arr[0] > arr[1], swap them.
  2. Compare arr[ane] with arr[2]. If arr[1] > arr[2], swap them.
  3. Finally, compare a[N-3] with arr[N-two], if arr[N-3] > arr[N-two], swap them.

This completes the 2nd passthrough. Afterwards, this passthrough, the 2d highest chemical element volition be at the second highest alphabetize in the assortment.

Notation that in the final stride of the 2d passthrough, we are not comparing the 2nd last element i.e a[North-two] with the last chemical element i.e arr[Due north-1], this is because the last chemical element is already in its correct position.

Passthrough #3:

  1. Compare arr[0] with arr[one]. If arr[0] > arr[1], swap them.
  2. Compare arr[1] with arr[2]. If arr[i] > arr[ii], bandy them.
  3. Finally, compare a[N-4] with arr[N-3], if arr[N-4] > arr[North-three], bandy them.

This completes the third passthrough. After, this passthrough, the third highest element will be at the third highest alphabetize in the array.

In this way, we keep passing through the array. Nosotros end when nosotros encountered a passthrough that's hasn't swapped any elements.

Allow's have an example.

Suppose, nosotros have an assortment arr declared and initialized equally:

                                            #define SIZE 5                      int                      arr                      [                      SIZE                      ]                      =                      {                      80                      ,                      threescore                      ,                      xc                      ,                      10                      ,                      40                      };                      // unsorted array                    

Here are the steps to sort this array in increasing order using bubble sort.

Passthrough #1:

Footstep one: Compare 80 and lx. Since, 80 > 60, swap them:

                        |            60            |            80            |            90            |            10            |            xl            |          

Stride 2: Compare fourscore and 90. Since, lxxx < xc, nosotros do nothing:

                        |            sixty            |            80            |            ninety            |            10            |            40            |          

Footstep 3: Compare 90 and x. Since, 90 > 10, bandy them:

                        |            threescore            |            80            |            x            |            90            |            40            |          

Step iv: Compare 90 and 40. Since, 90 > 40, swap them:

                        |            60            |            80            |            10            |            40            |            90            |          

This completes the showtime passthrough. The highest element i.east 90, is now at the finish of the array. We have made three swaps in this passthrough. So, we demand to perform some other passthrough. Continue in mind that we continue passing through the array until we meet a passthrough that hasn't swapped any elements.

Passthrough #two:

Step ane: Compare 60 and 80. Since, threescore < eighty, we practise nil:

                        |            lx            |            fourscore            |            ten            |            40            |            xc            |          

Footstep ii: Compare 80 and ten. Since, 80 > ten, swap them:

                        |            60            |            10            |            80            |            40            |            xc            |          

Step 3: Compare 80 and twoscore. Since, 80 > 40, swap them:

                        |            60            |            ten            |            40            |            80            |            90            |          

This completes the second passthrough. The second highest element i.e 80, is now at the second highest index in the array. Too, note that we haven't compared fourscore with 90. This is because the chemical element 90 is already in its correct position from passthrough #i.

We have made 2 swaps in this passthrough. And then, we need to perform another one.

Passthrough #3:

Step 1: Compare 60 and ten. Since, 60 > 10, swap them:

                        |            x            |            threescore            |            forty            |            80            |            ninety            |          

Stride two: Compare 60 and xl. Since, threescore > 40, swap them:

                        |            10            |            40            |            60            |            eighty            |            90            |          

This completes the third passthrough. The third highest element i.e 60, is now at the third highest index in the assortment. Too, note that we haven't compared 60 with lxxx. This is because the element eighty is already in its correct position from passthrough #2.

We take fabricated 2 swaps in this passthrough. And then, we need to perform another i.

Passthrough #4:

Step 1: Compare 10 and 40. Since, 10 < twoscore, nosotros do null:

                        |            10            |            40            |            lx            |            fourscore            |            90            |          

This completes the fourth passthrough. Nosotros haven't made any swaps in this passthrough. So, we need don't need to perform another one. All the elements in the array are at present sorted in ascending order.

Chimera Sort C Plan #

The following is a C program which sorts the array in ascending order using Bubble sort algorithm:

                      1  ii  iii  4  5  6  7  8  9 10 11 12 13 fourteen 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 threescore 61 62 63 64 65 66 67 68 69 seventy 71 72 73 74 75 76 77 78 79 80 81
                                            /****************************************************************                                              * Program to sort an assortment in ascending lodge using Bubble sort                                                                    ****************************************************************/                      #include                      <stdio.h> // include stdio.h library                                            #ascertain MAX 5                      void                      bubble_sort                      (                      int                      arr                      []);                      // function announcement                      int                      main                      (                      void                      )                      {                      int                      arr                      [                      MAX                      ];                      // input array                      for                      (                      int                      i                      =                      0                      ;                      i                      <                      MAX                      ;                      i                      ++                      )                      {                      printf                      (                      "arr[%d] = "                      ,                      i                      );                      scanf                      (                      "%d"                      ,                      &                      arr                      [                      i                      ]);                      }                      printf                      (                      "                      \n                      Unsorted array:                                            \due north                      "                      );                      // print unsorted assortment                      for                      (                      int                      i                      =                      0                      ;                      i                      <                      MAX                      ;                      i                      ++                      )                      {                      printf                      (                      "%d "                      ,                      arr                      [                      i                      ]);                      }                      // sort array                      bubble_sort                      (                      arr                      );                      printf                      (                      "                      \north\northward                      Sorted assortment:                                            \n                      "                      );                      // print sorted array                      for                      (                      int                      i                      =                      0                      ;                      i                      <                      MAX                      ;                      i                      ++                      )                      {                      printf                      (                      "%d "                      ,                      arr                      [                      i                      ]);                      }                      return                      0                      ;                      // render 0 to operating system                      }                      /*                                              *  bubble_sort() takes an array and sorts it                                              *  in the ascending order                                              */                      void                      bubble_sort                      (                      int                      arr                      [])                      {                      int                      tmp                      ,                      // temporary variable to hold one of the values while swapping                      is_swapped                      ;                      // variable to indicate whether we have fabricated any swaps during the passthrough                      for                      (                      int                      i                      =                      0                      ;                      i                      <                      MAX                      ;                      i                      ++                      )                      {                      // re-initialize is_swapped to 0 afterwards every passthrough                                            is_swapped                      =                      0                      ;                      for                      (                      int                      j                      =                      0                      ;                      j                      <                      MAX                      -                      1                      -                      i                      ;                      j                      ++                      )                      {                      if                      (                      arr                      [                      j                      ]                      >                      arr                      [                      j                      +                      1                      ])                      // compare adjacent elements                      {                      // swap side by side elements                      tmp                      =                      arr                      [                      j                      ];                      arr                      [                      j                      ]                      =                      arr                      [                      j                      +                      1                      ];                      arr                      [                      j                      +                      one                      ]                      =                      tmp                      ;                      // set is_swapped to 1, to bespeak                      // that we have made at least one                                            // bandy during the passthrough                      is_swapped                      =                      1                      ;                      }                      }                      // if no swaps are made in the last passthrough,                      // exit the outer for loop                      if                      (                      !                      is_swapped                      )                      {                      break                      ;                      }                      }                      }                    

Try it now

Expected Output:

                      1  2  3  four  5  6  7  8  9 ten 11 12
                      Enter assortment:  arr[0] = lxxx arr[one] = sixty arr[2] = 90 arr[iii] = x arr[4] = 40  Unsorted array:  lxxx 60 90 10 40   Sorted array:  10 xl 60 80 ninety                    

How information technology works #

All the work is done within the bubble_sort() role:

Here is how it works:

In lines 50 and 51, we have declared 2 variables: tmp and is_swapped. The tmp variable will hold one of the values while swapping the elements and is_swapped is used equally a flag to indicate whether we have made any swaps during the passthrough or non.

In lines 53-81, nosotros have an outer for loop, which runs until the assortment is non sorted.

The inner for loop in lines 58-72, swaps out of order elements during the passthrough. It runs from the starting time of the array and goes on until the index that hasn't been sorted yet.

If we fabricated at least ane bandy during the passthrough we ready is_swapped to 1 (line 70).

At last, the if argument in lines 77-eighty, checks the value of is_swapped to determine, whether to break out of the outer for loop or not. We interruption out of the outer for loop when we encountered a passthrough that'due south hasn't swapped any elements.

Keep in heed that the higher up function sorts the array in increasing order. To sort elements in decreasing club, simply modify the if condition in line 60 from arr[j] > arr[j+i] to arr[j] < arr[j+1].


Recommended Reading:

  • C Program to find the count of even and odd elements in the array
  • C Program to add ii Matrices
  • C Program to multiply two matrices
  • C Programme to notice the transpose of a matrix
  • C Programme to search for an detail using Linear Search
  • C Plan to search for an item using Binary Search


Ezoic

schrantzgred1938.blogspot.com

Source: https://overiq.com/c-examples/c-program-to-sort-an-array-in-ascending-order-using-bubble-sort/

Related Posts

0 Response to "Taking an Element From an Array Then Checking to Make Sure It Hasnt Been Chosen Again Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel