Download presentation

Presentation is loading. Please wait.

Published byMyra Heathcoat Modified over 2 years ago

1
Bubble Sort Algorithm 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.1Loop (i) from 0 to size of the list to be sorted - 2. 2.1.1Compare the i th and (i + 1) st elements in the unsorted list. 2.1.2Swap the i th and (i + 1) st elements if not in order ( ascending or descending as desired). 2.2Decrease the size of the list to be sorted by 1. 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: Each pass "bubbles" the largest element in the unsorted part of the list to its correct location. A13743531922329??

2
Bubble Sort Implementation 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. Here is an ascending-order implementation of the bubblesort algorithm for integer arrays:

3
Bubble Sort Trace Trace the given implementation on the array below. Try to keep track of how many comparisons and swaps are performed. A13743531922329?? 012345678 A13743531922329 Original array Pass 1 Pass 2 Pass 3 Pass 4 Pass 5 Pass 6 Pass 7 Pass 8 Pass 9

4
Selection Sort Algorithm 1. Loop (i) from 0 to the (number of elements to be sorted - 2) 1.1 Assume the smallest remaining item is at the i th 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 j th & smallest elements in the unsorted list. 1.2.2 If the j th element is < the smallest element then reset the location of the smallest to the j th 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. 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:

5
Selection Sort Implementation 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; } Here is an ascending-order implementation of the selection sort algorithm for integer arrays:

6
Selection Sort Trace Trace the given implementation on the array below. Try to keep track of how many comparisons and swaps are performed. A13743531922329?? 012345678 A13743531922329 Original array Pass 1 Pass 2 Pass 3 Pass 4 Pass 5 Pass 6 Pass 7 Pass 8 Pass 9

7
Floating Point Numbers a * 2 b (e.g..1011*2 3 ) 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) SS’723

8
Size Any no. < 10-38 gives UNDERFLOW Accuracy or Precision n 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 111001...11

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! binarydecimal.11/2.011/4.0011/8 1/10.0001100110011….0011 (23 bits)

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

11
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: would print: xxxxxx 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] << ' '; }

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

13
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 prints: s4: 012hijkl3456789 Note: a sequence of characters from a string is called a substring. For example: string s4 = "0123456789"; string s5 = "abcdefghijklmnopqrstuvwxyz"; s4.insert(3, s5, 7, 5); cout << "s4: " << s4 << endl;

14
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 prints: Fred Flintstone Flintstone For example: string s4 = "Fred Flintstone"; string s5 = s4.substr(5, 10); cout << s4 << endl << s5 << endl;

15
15 Erasing a Substring A substring may be deleted from a string by using the member function: would print: s6: abcijklmnopqrstuvwxyz 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;

Similar presentations

OK

Sorting A fundamental operation in computer science (many programs need to sort as an intermediate step). Many sorting algorithms have been developed Choose.

Sorting A fundamental operation in computer science (many programs need to sort as an intermediate step). Many sorting algorithms have been developed Choose.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on networking related topics about global warming Ppt on covalent bonding animation Ppt on regional transport office lucknow Seminar ppt on smart card technology Ppt on carl friedrich gauss formula Ppt on landing gear system for motorcycles Ppt on different forms of power sharing in india Ppt on index of industrial production definition Ppt on current account deficit meaning Ppt on cross-sectional study vs cohort study