Bubble Sort Algorithm One of the simplest sorting

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 sizeelement of the list beunsorted bypart 1. of the list to its correct location. Each pass "bubbles" the largest in to the A 13 7 43 5 3 19 2 23 29 ? ? ? ?

Bubble Sort Implementation Here is an ascending-order implementation of the bubblesort algorithm for integer arrays: void Bubble. Sort(int List[] , int Size) { int temp. Int; // temp variable for swapping list elems for (int Stop = Size - 1; Stop > 0; Stop--) { pass for (int Check = 0; Check < Stop; Check++) { // make a if (List[Check] > List[Check + 1]) { } } // compare elems temp. Int = List[Check]; // swap if in the List[Check] = List[Check + 1]; // wrong order List[Check + 1] = temp. Int; 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.

Bubble Sort Trace the given implementation on the array below. Try to keep track of how many comparisons and swaps are performed. 0 A 13 1 7 A 13 2 3 43 5 7 4 3 43 5 19 5 6 2 3 19 7 8 23 29 2 23 29 Original array Pass 1 Pass 2 Pass 3 Pass 4 Pass 5 Pass 6 Pass 7 Pass 8 Pass 9 ? ? ? ?

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 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 smallest. 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.

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

Selection Sort Trace the given implementation on the array below. Try to keep track of how many comparisons and swaps are performed. 0 A 13 1 7 A 13 2 3 43 5 7 4 3 43 5 19 5 6 2 3 19 7 8 23 29 2 23 29 Original array Pass 1 Pass 2 Pass 3 Pass 4 Pass 5 Pass 6 Pass 7 Pass 8 Pass 9 ? ? ? ?

Floating Point Numbers a * 2 b (e. g. . 1011*23) a is mantissa and 0. 5<= a < 1. 0 b is exponent 32 bit word S S’ 7 23 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)

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

Question: Is ten one tenths equal to 1? Answer: Not in digital computing. binary decimal . 1 1/2 . 01 1/4 . 001 1/8 1/10 . 000110011…. 0011 (23 bits) Multiply this by 10 or add it 10 times to get. 11111…. 1111 which is not 1!

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 s 1 = "mairsy doates and doesy doates"; char ch 1 = s 1. at(5); // ch 1 == 'y' Note that the positions in a string are numbered sequentially, starting at zero. So: for (int i = 7; i <= 12; i++) cout << s 1. at(i) << ' '; would print: d o a t e s 10

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 s 1 = "mairsy doates and doesy doates"; char ch 1 = s 1[5]; // ch 1 == '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++) { s 1[i] = 'x'; cout << s 1[i] << ' '; } would print: x x x 11

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: // s: position at which insert begins string to be inserted For example: string Name = "Fred Flintstone"; string Middle. Initial = " G. "; Name. insert(4, Middle. Initial); cout << Name << endl; prints: Fred G. Flintstone The function returns (a reference to) the string s 1 which can be assigned to another string variable if desired; but the content of the original string is changed in any case. 12

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

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: // numtocopy: position at which substring begins length of substring For example: string s 4 = "Fred Flintstone"; string s 5 = s 4. substr(5, 10); cout << s 4 << endl << s 5 << endl; prints: Fred Flintstone 14

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