Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 03.

Similar presentations


Presentation on theme: "CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 03."— Presentation transcript:

1 CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 03

2 Overview Search a 1D array Sort a 1D array 2D arrays

3 Linear Search Goal: Find the location of a particular element in an array Process: Start at the beginning, compare each element in the array to the value being searched for Remember: Array’s aren’t self aware of their size.

4 Source Code int linSearch(const int arr[], int key, int arrSize) { for (int j = 0; j < arrSize; j++) if(arr[j] == key) return j; return -1; }

5 Binary Search Input: Sorted Array Goal: Find the location of an element in an array if it exists Process: Compare key with middle element in the array. – If key < middle, element must be in lower half of array – if key > middle, element must be in upper half of array – else key found

6 Sorting Classic problem in CS and very well studied Probably hundreds of sorting algorithms out there. Typically, sorted data is “easier” to work with than unsorted data Insertion Sort: – Process: Take 2 nd element and swap with 1 st if it is smaller. Take 3 rd element, insert into proper position with respect to 1 st and 2 nd. and so on…

7 2D Arrays 20 x 20 grid of integers access each element using 2 subscripts – cout << data[1][2]; – cin >> data[2][10]; Don’t make the mistake of – cout << data[1, 2]; int data[20][20];

8 2D array as matrix Write a program to read 2 matrices (of compatible size) out of a file and add them together.

9 ?


Download ppt "CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 03."

Similar presentations


Ads by Google