Download presentation
Presentation is loading. Please wait.
Published byLesley Skinner Modified over 6 years ago
1
CS1010 Discussion Group 11 Week 8 – Searching and Sorting
2
Slides are at http://www.comp.nus.edu.sg/~yanhwa/
HELLO! Slides are at
3
Midterms Midterm papers! Hexagons
4
When does a game end for the frogs?
Lab Lab 4 due on Wed! When does a game end for the frogs?
5
diff myans.out teetoo.out No news is good news
Lecture Summary UNIX I/O redirection Input redirection Output redirection Fails if output already exist. To append: diff myans.out teetoo.out No news is good news a.out < teetoo.in a.out > myans.out a.out >> numbers Helps with doing lab assignments if you haven’t been using this! A sudden drop in pacing before Pointers… A calm before the storm?
6
Print-f, wolf-fencing, logging
Lecture Summary Debugging Manual walkthroughs Tracing and talking to yourself Print-f, wolf-fencing, logging Provides the “flow” of the program. Print immediate results. Can be messy and tedious Possible to make the logging look cleaner: Sometimes print different “logging levels” with different colours printf(“[function_name] var_name : %d”, var); printf(“[calc_sum] sum: %d”, sum);
7
Test boundaries, exceptions, special cases Incremental coding
Lecture Summary Debugging Test boundaries, exceptions, special cases Negative number, zero, edge case Incremental coding Stubs. Another function that we want to test relies on this function. This function must work too. Thus it should just return some value first. Debugger tool Useful especially when you have a lot of variables to track, huge arrays, complicated program etc.
8
Tutorial 3 – Modified sorting condition
// To sort arr in increasing order. void selectionSort(int arr[], int size) { int i, start_index, min_index, temp; for (start_index = 0; start_index < size-1; start_index++) { // each iteration of the for loop is one pass // find the index of minimum element min_index = start_index; for (i = start_index+1; i < size; i++) { if (arr[i] < arr[min_index]) min_index = i; } // swap minimum element with element at start_index temp = arr[start_index]; arr[start_index] = arr[min_index]; arr[min_index] = temp; Change which line?
9
Tutorial 3 What is in the function lessthan(a[i], a[j])?
Lessthan returns true when a[i] < a[j]. What other function do we need? // Return 1st 3 digits of n int first3digits(int n) { while (n > 999) n /= 10; return n; }
10
Tutorial 4 // To sort arr in increasing order.
void bubbleSort(int arr[], int size) { int i, limit, temp; for (limit = size-2; limit >= 0; limit--) { // limit is where the inner loop variable i should end for (i=0; i<=limit; i++) { if (arr[i] > arr[i+1]) { // swap arr[i] with arr[i+1] temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; } } //end of inner loop What does limit represent at every iteration?
11
Tutorial 4 – Modified bubble sort
// To sort arr in increasing order. void bubbleSort(int arr[], int size) { int i, limit, temp; for (limit = size-2; limit >= 0; limit--) { // limit is where the inner loop variable i should end for (i=0; i<=limit; i++) { if (arr[i] > arr[i+1]) { // swap arr[i] with arr[i+1] temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; } } //end of inner loop int swap_encountered = 1; limit = size - 2; while (swap_encountered && (limit >= 0)) { swap_encountered = 0; swap_encountered = 1; How do we know if an array is “already sorted” and we can stop sorting prematurely? limit--; No more swaps at a iteration of the outer loop!!
12
Tutorial 5 – Insertion sort
// To sort arr in increasing order. void insertionSort(int arr[], int size) { int i, j, temp; for (i=1; i<size; i++) { temp = arr[i]; j = i-1; while ((j>=0) && (temp<arr[j])) { arr[j+1] = arr[j]; j--; } arr[j+1] = temp; Loop invariant: Array is sorted from A[0] to A[i] Shifting these elements that are bigger than temp forward to leave space for temp
13
Tutorial 5
14
Tutorial 6 – Duplicates // Count the number of duplicates in array arr
// This algorithm uses the fact that the array is sorted. // Precond: arr is sorted in increasing order // size > 0 int countDuplicates(int arr[], int size) { int count = 0, i; int newDuplicate = 0; // check if it is a newly spotted duplicate for (i=1; i<size; i++) { if (arr[i] == arr[i-1]) { count++; if (newDuplicate == 0) { count++; // Notice that when we first spot a duplicate, we do count++ twice newDuplicate = 1; } else { newDuplicate = 0; return count;
15
Tutorial 7 Scan minefields Print minefields (for checking) Scan pattern Print pattern (for checking) Search for pattern in minefields
16
Tutorial 7 For example, row and col represents A[row][col] that we want to search. At first, row = 0, col = 0. When to stop increasing row? When to stop increasing col? How to ensure it is exactly like pattern?
17
Tutorial 7 ………instead of breaks Can consider using a while loop.
Breaks were used here because it is more clear how we are iterating through the “matrix” with a for loop
18
Extra
19
Midterms Midterm questions?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.