Bubble sort is the simplest and slowest one uses the

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ort is the simplest and slowest one. Uses the Archimedes Principle: lighter materials should rise to rest on top of heavier materials. The bubble sort algorithm is given below: { The algorithm consists of two for loops, one nested inside the other. { In each pass of the outer loop, the next \lightest" element is \bubbled" to as far \top" as possible. The inner loop is responsible for the bubbling process. { The inner loop always starts from the \bottom" and tries to move \lighter" elements in the upward direction by comparing and possibly swapping adjacent elements. CSC 1500 { Lecture 11 3 #include <stdio.h> #include <string.h> void bubble_sort_int(int int main(void) { int int ], int) list ]={7, 3, 66, 3, -5, 22, -77, 2} i bubble_sort_int(list, 8) return 0 } void bubble_sort_int(int a ], int size) { int i, j, temp for (i=0 i < size-1 i++) j--) /* perform (size-1) rounds */ /* starting from the end of the array up to the first UNSETTLED element in current round */ /* compare two adjacent elements */ /* swap their content in case */ /* to make them in order */ for (j=size-1 j>i if (a j-1] > a j]) { temp = a j-1] a j-1] = a j] a j] = temp } } CSC 1500 { Lecture 11 4 i=0 j j j j j j j i=1 j j j j j j i=2 j j j j j i=3 j j j j i=4 j=7 j=6 j=5 i=5 j=7 j=6 i=6 j=7 -77 -5 2 3 3...
View Full Document

This note was uploaded on 05/23/2010 for the course COMPUTER S CSC1500 taught by Professor Fung during the Spring '10 term at CUHK.

Ask a homework question - tutors are online