Download presentation
Presentation is loading. Please wait.
1
Module 8 – Searching & Sorting Algorithms
Ps Module 8 – Searching & Sorting Algorithms 4/25/2019 CSE 1321 Module 8
2
Linear Search Algorithm
METHOD LinearSearch (parameter: list G, target) BEGIN FOR each element temp in list G IF (temp == target) RETURN true ENDIF ENDFOR RETURN false END LinearSearch Ps 5 4/26/2018 CSE 1321 Module 8
3
Python Linear Search Algorithm
def linear_search(searchArray, item, start=0): for i in range(start, len(searchArray)): if searchArray[i] == item: return i return -1 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] results = linear_search(numbers, 7, start=0) if results > 0: print("Number 7 found at: ", results) else: print("Number 7 not found!") 8 4/26/2018 CSE 1321 Module 8
4
Binary Search Algorithm
METHOD BinarySearch (parameters: array G, target) BEGIN low ← 0, mid ← 0, high ← the number of elements in G WHILE (true) mid ← (low + high) / 2 IF (target = G[mid]) RETURN true ELSE IF (target < G[mid]) high ← mid ELSE low ← mid ENDIF IF (mid+1 >= high) RETURN false ENDWHILE END BinarySearch Ps 4/26/2018 CSE 1321 Module 8
5
Python Binary Search Algorithm
def binarySearch(alist, item): first = 0 last = len(alist)-1 found = False while first<=last and not found: midpoint = (first + last)//2 if alist[midpoint] == item: found = True else: if item < alist[midpoint]: last = midpoint-1 first = midpoint+1 return found testlist = [0, 1, 2, 8, 13, 17, 19, 32, 42,] print(binarySearch(testlist, 3)) print(binarySearch(testlist, 13)) 13 4/26/2018 CSE 1321 Module 8
6
Exchange Sort Algorithm
// You should take the time to trace through this FOR each I from 1 to n FOR each J from I+1 to n IF (A[J] < A[I]) temp ← A[J] A[J] ← A[I] A[I] ← temp ENDIF ENDFOR Ps CSE 1321 Module 8 17 4/26/2018
7
Python Bubble Sort Algorithm
def bubbleSort(unsorted): for i in range(len(unsorted)): for j in range(0,len(unsorted)-i-1): if unsorted[j] > unsorted[j+1]: temp = unsorted[j] unsorted[j] = unsorted[j+1] unsorted[j+1] = temp myArray = [54,26,93,17,77,31,44,55,20] bubbleSort(myArray) print(myArray) CSE 1321 Module 8 20 4/26/2018
8
Selection Sort Algorithm
FOR each I from 0 to n minPos ← I FOR each J from I + 1 to n IF (B[j] < B[minPos]) minPos ← J ENDIF ENDFOR IF (I != minPos AND minPos < n) temp ← B[minPos] B[minPos] ← B[I] B[I] ← temp Ps CSE 1321 Module 8 23 4/26/2018
9
Python Selection Sort Algorithm
def selectionSort(unsorted): for i in range(len(unsorted)): positionOfMin=i for j in range(i+1,len(unsorted)): if unsorted[positionOfMin]>unsorted[j]: positionOfMin = j temp = unsorted[i] unsorted[i] = unsorted[positionOfMin] unsorted[positionOfMin] = temp myArray = [54,26,93,17,77,31,44,55,20] selectionSort(myArray) print(myArray) CSE 1321 Module 8 26 4/26/2018
10
Insertion Sort Algorithm
FOR each index from 2 to n key ← A[index] position ← index // Shift larger values to the right WHILE (position > 0 AND key < A[position-1]) A[position] = A[position - 1] position ← position -1 ENDWHILE list [position] = key ENDFOR Ps CSE 1321 Module 8 30 4/26/2018
11
Python Insertion Sort Algorithm
def insertionSort(unsorted): for index in range(1,len(unsorted)): currentvalue = unsorted[index] position = index while position>0 and unsorted[position-1]>currentvalue: unsorted[position]=unsorted[position-1] position = position-1 unsorted[position]=currentvalue myArray = [54,26,93,17,77,31,44,55,20] insertionSort(myArray) print(myArray) CSE 1321 Module 8 33 4/26/2018
12
Sorting in a Python Program
sorted(iterable[, cmp[, key[, reverse]]]) Return a new sorted list from the items in iterable. The optional arguments cmp, key, and reverse have the same meaning as those for the list.sort() method (described in section Mutable Sequence Types). cmp specifies a custom comparison function of two arguments (iterable elements) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: cmp=lambda x,y: cmp(x.lower(), y.lower()). The default value is None. key specifies a function of one argument that is used to extract a comparison key from each list element: key=str.lower. The default value is None (compare the elements directly). reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed. In general, the key and reverse conversion processes are much faster than specifying an equivalent cmp function. This is because cmp is called multiple times for each list element while key and reverse touch each element only once. Use functools.cmp_to_key() to convert an old-style cmp function to a key function. The built-in sorted() function is guaranteed to be stable. A sort is stable if it guarantees not to change the relative order of elements that compare equal — this is helpful for sorting in multiple passes (for example, sort by department, then by salary grade). CSE 1321 Module 8 36 4/26/2018
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.