190 likes | 194 Views
This discussion group covers topics such as searching and sorting algorithms, input/output redirection, debugging techniques, and lab assignments.
E N D
CS1010 Discussion Group 11 Week 8 – Searching and Sorting
HELLO! Slides are at http://www.comp.nus.edu.sg/~yanhwa/
Midterm papers! Hexagons Midterms
Lab 4 due on Wed! When does a game end for the frogs? Lab
UNIX I/O redirection Lecture Summary • Input redirection • Output redirection • Fails if output already exist. To append: • diff myans.outteetoo.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?
Debugging Lecture Summary • 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);
Debugging Lecture Summary • 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.
Tutorial 3 – Modified sorting condition • // To sort arr in increasing order. • voidselectionSort(intarr[], intsize) { • inti, 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?
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; }
Tutorial 4 // To sort arr in increasing order. voidbubbleSort(intarr[], intsize) { inti, 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?
Tutorial 4 – Modified bubble sort // To sort arr in increasing order. voidbubbleSort(intarr[], intsize) { inti, 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 } } intswap_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? No more swaps at a iteration of the outer loop!! limit--;
Tutorial 5 – Insertion sort // To sort arr in increasing order. voidinsertionSort(intarr[], intsize) { inti, 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
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 intcountDuplicates(intarr[], intsize) { intcount = 0, i; intnewDuplicate = 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; } } returncount; }
Tutorial 7 Scan minefields Print minefields (for checking) Scan pattern Print pattern (for checking) Search for pattern in minefields
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?
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
Extra https://www.youtube.com/watch?v=6LOwPhPDwVc
Midterm questions? Midterms