# Bubble sort is the simplest and slowest one uses the

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

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

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern