Bubble Sort Algorithm for (int outer=0; outer<a.length-1; - - PowerPoint PPT Presentation

bubble sort algorithm
SMART_READER_LITE
LIVE PREVIEW

Bubble Sort Algorithm for (int outer=0; outer<a.length-1; - - PowerPoint PPT Presentation

Bubble Sort Algorithm for (int outer=0; outer<a.length-1; outer++) { for (int inner=0; inner<a.length-1-outer; inner++) { if (a[inner+1] < a[inner]) { int tmp = a[inner]; a[inner] = a[inner+1]; a[inner+1] = tmp; }}} Bubble Sort


slide-1
SLIDE 1

Bubble Sort Algorithm

for (int outer=0; outer<a.length-1;

  • uter++) {

for (int inner=0; inner<a.length-1-outer; inner++) { if (a[inner+1] < a[inner]) { int tmp = a[inner]; a[inner] = a[inner+1]; a[inner+1] = tmp; }}}

slide-2
SLIDE 2

Bubble Sort

Number of comparisons Number of swaps

average case worst case

n(n-1) / 2 n(n-1) / 4 n(n-1) / 2

slide-3
SLIDE 3

Selection Sort Algorithm

for (int outer = 0; outer < a.length-1;

  • uter++) {

int min = outer; for (int inner=outer+1; inner <a.length; inner++) { if (a[inner] < a[min]) min = inner; } int temp = a[outer]; a[outer] = a[min]; a[min] = temp; }}

slide-4
SLIDE 4

Selection Sort

Number of comparisons Number of swaps

average case worst case

n(n-1) / 2 n/2 n

slide-5
SLIDE 5

Insertion Sort Algorithm

for(int outer=1; outer<a.length; outer++) { temp = a[outer]; int inner=outer; for (; ((inner>0)&&(a[inner-1]>temp)) inner--) { a[inner] = a[inner-1]; } a[inner] = temp; }}

slide-6
SLIDE 6

Insertion Sort

Number of comparisons

worse case average case

Number of copies/shifts, same as comparison

n ( n – 1) / 2 n ( n – 1) / 4