1 / 15

Bubble Sort Algorithm

Bubble Sort Algorithm. One of the simplest sorting algorithms proceeds by walking down the list, comparing adjacent elements, and swapping them if they are in the wrong order. The process is continued until the list is sorted. More formally:.

nona
Download Presentation

Bubble Sort Algorithm

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Bubble Sort Algorithm One of the simplest sorting algorithms proceeds by walking down the list, comparing adjacent elements, and swapping them if they are in the wrong order. The process is continued until the list is sorted. More formally: 1. Initialize the size of the list to be sorted to be the actual size of the list. 2. Loop through the list until no element needs to be exchanged with another to reach its correct position. 2.1 Loop (i) from 0 to size of the list to be sorted - 2. 2.1.1 Compare the ith and (i + 1)st elements in the unsorted list. 2.1.2 Swap the ith and (i + 1)st elements if not in order ( ascending or descending as desired). 2.2 Decrease the size of the list to be sorted by 1. Each pass "bubbles" the largest element in the unsorted part of the list to its correct location.

  2. Bubble Sort Implementation Here is an ascending-order implementation of the bubblesort algorithm for integer arrays: void BubbleSort(int List[] , int Size) { int tempInt; // temp variable for swapping list elems for (int Stop = Size - 1; Stop > 0; Stop--) { for (int Check = 0; Check < Stop; Check++) { // make a pass if (List[Check] > List[Check + 1]) { // compare elems tempInt = List[Check]; // swap if in the List[Check] = List[Check + 1]; // wrong order List[Check + 1] = tempInt; } } } } Bubblesort compares and swaps adjacent elements; simple but not very efficient. Efficiency note: the outer loop could be modified to exit if the list is already sorted.

  3. Bubble Sort Trace Trace the given implementation on the array below. Try to keep track of how many comparisons and swaps are performed.

  4. Selection Sort Algorithm Another simpe sorting algorithm proceeds by walking down the list, and finding the smallest (or largest) element, and then swapping it to the beginning of the unsorted part of the list. The process is continued until the list is sorted. More formally: 1. Loop (i) from 0 to the (number of elements to be sorted - 2) 1.1 Assume the smallest remaining item is at the ith position, call this location smallest. 1.2 Loop (j) through the remainder of the list to be sorted (i+1 .. size-1). 1.2.1 Compare the jth & smallest elements in the unsorted list. 1.2.2 If the jth element is < the smallest element then reset the location of the smallest to the jth location. 1.3 Move the smallest element to the head of the unsorted list, (i.e. swap the ith and smallest elements). After sorting all but 1 element the remaining element must be in its correct position.

  5. Selection Sort Implementation Here is an ascending-order implementation of the selection sort algorithm for integer arrays: void SelectionSort(int List[], int Size) { int Begin, SmallSoFar, Check; void Swap(int& Elem1, int& Elem2); // see previous slide for (Begin = 0; Begin < Size - 1; Begin++) { SmallSoFar = Begin; // set head of tail for (Check = Begin + 1; Check < Size; Check++) { // scan current tail if (List[Check] < List[SmallSoFar]) SmallSoFar = Check; } Swap(List[Begin], List[SmallSoFar]); // put smallest elem at front } // of current tail } void Swap(int& Elem1, int& Elem2) { int tempInt; tempInt = Elem1; Elem1 = Elem2; Elem2 = tempInt; }

  6. Selection Sort Trace Trace the given implementation on the array below. Try to keep track of how many comparisons and swaps are performed.

  7. Floating Point Numbers a * 2b (e.g. .1011*23) a is mantissa and 0.5<= a < 1.0 b is exponent 32 bit word sign of mantissa exponent mantissa SIZE of floating point number governed by EXPONENT PRECISION of floating point number governed by MANTISSA - (# of decimal places results are accurate to)

  8. Size Any no. < 10-38 gives UNDERFLOW Accuracy or Precision • Mantissa . 23 binary bits = ? decimal digits 10 binary bits is equivalent to 3 decimal digits, so 23 binary bits is equivalent to 7 deciaml digits

  9. Question: Is ten one tenths equal to 1? Answer: Not in digital computing. Multiply this by 10 or add it 10 times to get .1111111111….1111 which is not 1!

  10. Accessing String Elements A copy of the character at a particular position in a string variable may be obtained by using the member function: char at(int position); // position: position of desired element For example: string s1 = "mairsy doates and doesy doates"; char ch1 = s1.at(5); // ch1 == 'y' Note that the positions in a string are numbered sequentially, starting at zero. So: for (int i = 7; i <= 12; i++) cout << s1.at(i) << ' '; would print: d o a t e s

  11. Accessing String Elements The character at a particular position in a string variable may also be referenced by using an index with the string object, similar to an array access. For example: string s1 = "mairsy doates and doesy doates"; char ch1 = s1[5]; // ch1 == 'y' The primary difference between at() and [] with string variables is that [] returns a reference to the string element, so: for (int i = 7; i <= 12; i++) { s1[i] = 'x'; cout << s1[i] << ' '; } would print: x x x x x x

  12. Inserting One string into Another A string of characters may be inserted at a particular position in a string variable by using the member function: string& insert(int startinsert, string s); // startinsert: position at which insert begins // s: string to be inserted For example: string Name = "Fred Flintstone"; string MiddleInitial = " G."; Name.insert(4, MiddleInitial); cout << Name << endl; prints: Fred G. Flintstone The function returns (a reference to) the strings1 which can be assigned to another string variable if desired; but the content of the original string is changed in any case.

  13. Inserting a Part of one String into Another Another version of the insert function takes four parameters: string& insert(int startinsert, string s, int startcopy, int numtocopy); // startinsert: position at which insert begins // s: string to be inserted // startcopy: position (in s) of first element to be used // numtocopy: number of elements (of s) to be used For example: string s4 = "0123456789"; string s5 = "abcdefghijklmnopqrstuvwxyz"; s4.insert(3, s5, 7, 5); cout << "s4: " << s4 << endl; prints: s4: 012hijkl3456789 Note: a sequence of characters from a string is called a substring.

  14. Extracting a Substring A substring of a string may be extracted (copied) and assigned to another by using the member function: string& substr(int startcopy, int numtocopy); // startcopy: position at which substring begins // numtocopy: length of substring For example: string s4 = "Fred Flintstone"; string s5 = s4.substr(5, 10); cout << s4 << endl << s5 << endl; prints: Fred Flintstone Flintstone

  15. Erasing a Substring A substring may be deleted from a string by using the member function: string& erase(int starterase, int numtoerase); // starterase: position of first element to be erased // numtoerase: number of elements to be erased For example: string s6 = "abcdefghijklmnopqrstuvwxyz"; s6.erase(3, 5); cout << "s6: " << s6 << endl; would print: s6: abcijklmnopqrstuvwxyz

More Related