Which code block uses bubble sort?

Dyskusje na temat Javy.
jordana
Nowicjusz
Posty: 1
Dołączył(a): poniedziałek, 14 września 2020, 16:53
Contact:

Which code block uses bubble sort?

Post przez jordana »

Hey,
May I know which one is bubble sort from here:

1)

Code: Zaznacz cały

for(int i=0;i<n-1;i++)
  for(int j=n-1; j>i;j--)
    if(a[j] < a[j-1])
        swap(a[j], a[j-1]);
2)

Code: Zaznacz cały

for(int i=0; i<n-1; i++)
  for(int j=i+1; j<n; j++)
    if(a[j] < a[i])
        swap(a[j],a[i]);
3)

Code: Zaznacz cały

int temp, i, j = 0;
    boolean swaped = true;

    while (swaped) {
        swaped = false;
        j++;
        for(i = 0; i < arr.length - j; i++){
            if(arr[i] > arr[i+1]){
                temp = arr[i];
                arr[i] = arr[i+1];
                arr[i+1] = temp;
                swaped = true;
            }
        }
    }
IMO, the first is bubble sort, not sure of the second and third.
Live Life With No Regrets
Post Reply