Download presentation
Presentation is loading. Please wait.
Published bySharon Richardson Modified over 9 years ago
1
Programming Training Main Points: - Sorting and searching algorithms on Arrays. - Problem Solving
2
Working with Python Files How to work with Files in Python? - open the file for the needed operation. f = open(’filename.txt’, ‘how’) filename.txt the name of the file how is a combination of r/w/a, t/b - do the reading and writing as it is required f.read() read the whole file f.readline() read a single line as a string + \n f.write(str) write a str to a file - close the file. f.close()
3
Working with Python Files Example: 1)Read one integer from a file ‘input.txt’ f = open(‘input.txt’, ‘rt’) a = int(f.readline().strip()) f.close() 2) Read three integers from a single line of the file ‘input.txt’ f = open(‘input.txt’, ‘rt’) a, b, c = map(int, f.readline().strip().split()) f.close()
4
Working with Python Files Example: 3) Read an array from the file. The file contains the number of elements on the first line and then the array elements on each line. f = open(‘input.txt’, ‘rt’) n = int(f.readline().strip()) array = [] for i in range(n): elem = int(f.readline().strip()) array.append(elem) f.close()
5
Bubble Sorting Algorithm Sorting Problem: Given a list l = [l[i], i=0,1,…,n-1] with n integer elements then change/swap the elements so that they are in good order. Classical problem with several solutions: - Bubble, Ranking or Insert sorts are non optimal - Quick, Merge or Heap sorts are optimal sometimes difficult to implement.
6
Bubble Sorting Algorithm Swap two variables a, b Must use an extra variable and the chain of assignments tmp = a a =b b = tmp Phyton solution to swap a, b a, b = b, a Compare and exchange two variables a, b make always a<b (denote that by a↔b) if a>b: a, b = b, a # endif Suppose that l is a list with n elements.What is the result of the chain of compare and exchange l[0] ↔l[1] ↔l[2] ↔ … ↔l[n-1]? The maximum elements gets on its last position.
7
Bubble Sorting Algorithm Suppose that l is a list with n elements. What is the result of the following sequence l[0] ↔l[1] ↔l[2] ↔ … ↔l[n-1] … l[0] ↔l[1] ↔l[2] ↔ … ↔l[n-1] The list gets sorted out. sort(l) uses n*n compare and exchange operations. def sort(l): n = len(l) for i in range(n): for j in range(n): if l[j]>l[j+1] : tmp = l[j] l[j] =l[j+1] l[j+1] = tmp #endif #endfor
8
Linear Search Algorithm Search Problem: Given a list l = [l[i], i=0,1,…,n-1] with n integer elements then search whether the element x belongs to the list. The element position is given by the variable pos where pos = -1 when x does not belong to l pos = i when x belongs to l and x = l[i] Python solution is given by “x in l” but this does not tell the position of x on l. 1)Initialize pos = -1 2)Traverse the list and compare X with the current elements def search(x, l): n = len(l) pos = -1 for i in range(n): if x == l[i]: pos = I break #endif #endfor #end search
9
To do List 1.Solve the HW problems. 2.Read more about sorting algorithms
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.