1 / 19

CS1010 Discussion Group 11

This discussion group covers topics such as searching and sorting algorithms, input/output redirection, debugging techniques, and lab assignments.

Download Presentation

CS1010 Discussion Group 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1010 Discussion Group 11 Week 8 – Searching and Sorting

  2. HELLO! Slides are at http://www.comp.nus.edu.sg/~yanhwa/

  3. Midterm papers! Hexagons Midterms

  4. Lab 4 due on Wed! When does a game end for the frogs? Lab

  5. 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?

  6. 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);

  7. 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.

  8. 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?

  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. 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?

  11. 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--;

  12. 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

  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 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; }

  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 https://www.youtube.com/watch?v=6LOwPhPDwVc

  19. Midterm questions? Midterms

More Related