简体   繁体   中英

Bubble Sort using Bubble Up

Given the algorithm for Bubble Sort:

Algorithm BubbleSort(A[0...n]):
  for i <- 0 to n-2 do
    for j <- 0 to n-2-i do
      if(A[j+1] < A[j] then swap(A[j], A[j+1]))

I have to rewrite the Bubble Sort algorithm using where we "Bubble Up" the smallest element to the ith position on the ith pass through the list.

Can anyone help me with this?

#include<stdio.h>
void bubbleSort(int *x,int size)
{
  int e,f,m,g;
  m=size-2;
  while(m>0)
  {
    e=0;
    f=1;
    while(e<=m)
    {
      if(x[f]<x[e])  
      {
        g=x[e];       //swaping
        x[e]=x[f];
        x[f]=g;
      }
    e++;
    f++;   
    }
   m--; 
  } 
}
void main()
{
  int x[10],y;
  for(y=0;y<=9;y++)      //loop to insert 10 numbers into an array
  {
    printf("Enter a number: ");
    scanf("%d",&x[y]);
  }
  bubbleSort(x,10);      //pass number entered by user and size of array to bubbleSort 
  for(y=0;y<=9;y++)     //loop to print sorted numbers
  {
    printf("%d\n",x[y]);
  }
}

Currently you are traversing the array from the start, therefore if you come upon the largest element, it will be "Bubbled up" to the end of the array. If you want to do the opposite, "Bubbling down" the smallest element to the start, you need to traverse the array in the opposite direction, from the end to the start. Hope it helps you to find the way.

Looks like the answer to this has not been accepted yet. Hence trying to check if this is still an issue.

Here is what I think can be a possible implementation in Java. As @Warlord mentioned, the algorithm is to ensure that the array in concern for sorting is imagined as a vertical array. With each pass, all we are doing is check if there is a larger element below and if found that element is bubbled up to the top.

    static void bubbleUpSort(int[] arr){
    final int N = arr.length;
    int tmp = 0;
    for (int i=0; i < N; i++){
        for (int j=N-1; j >= i+1; j--){
            if (arr[j] < arr[j-1]){
                tmp = arr[j];
                arr[j] = arr[j-1];
                arr[j-1] = tmp;
            }
        }
    }

    for (int k =0; k < arr.length; k++){
        System.out.print(arr[k] + " ");
    }
}

Called from main as:

public static void main(String[] args) {
    System.out.println("Bubble Up Sort");
    int[] bUp = {19, 2, 9, 4, 7, 12, 13, 3, 6};
    bubbleUpSort(bUp);
}

Bubble Sort

Comparing each with the neighbor and swapping if first is greater than the next

function bubbleSort(arr){
  let temp;
  console.log("Input Array");
  console.log(arr);
  for (let i = 0; i < arr.length-1; i++) {
    for (let j = 0; j < arr.length-i-1; j++) {
      if (arr[j] > arr[j+1]) {
        temp = arr[j];
        arr[j] = arr[j+1];
        arr[j+1] = temp;
        console.log(arr[j],"swapped with",arr[j+1])
        console.log(arr);
      } else {
        console.log("SKIP");
      }
    }
  }
  console.log("Sorted using Bubble Sort");
  return arr;
}
console.log(bubbleSort([7,6,9,8,2,1,4,3,5]));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM