Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.

Similar presentations


Presentation on theme: "Dr. Sajib Datta CSE 1320 Arrays, Search and Sort."— Presentation transcript:

1 Dr. Sajib Datta CSE@UTA CSE 1320 Arrays, Search and Sort

2 Multidimensional Array We can also have arrays of arrays, also known as multidimensional arrays. E.g., A two-dimensional array is an array of several one- dimensional arrays. The basic form for declaring a 2D array is type array_name[rows][columns]; The mathematical equivalent of a two-dimensional array is a matrix.

3 2-D Array A 2D array can be viewed like a table. To create a 2D array for this table of values, we could use int some_data[2][3] = { {10, 5, 13}, {2, 7, 19} }; Or int some_data[][3] = { {10, 5, 13}, {2, 7, 19} }; 10513 2719

4 2-D Array As with one-dimensional arrays, 2D arrays indices begin at zero. The difference is now we have to keep track of indices in two dimensions. So, if we create the following array: int Sales[2][3] = { {1, 2, 3}, {4, 5, 6} }; The individual elements are referenced with the following combinations of array name and indices:

5 2-D Array Print out a specific row or column in an array #include int main(void) { int row, column, Sales[2][3] = { {1, 2, 3}, {4, 5, 6} }; printf("The values on row 0 are : \n"); for (column = 0; column < 3; column++) printf("%d ", Sales[0][column] ); printf("\n"); printf("The values on column 0 are: \n"); for (row = 0; row < 2; row++) printf("%d\n", Sales[row][0]); return 0; }

6 Copying an array In case of normal variables: to assign value of ‘a’ to ‘b’; int a,b; a=10; b = a; Assume we want to copy the values of array ‘a’ to array ‘b’ int a[10], b[10]; Initialize a; b = a;//wrong

7 Copying an array Have to iterate over ‘a’ and assign individual array elements into ‘b’ for(i=0;i<10;i++) b[i] = a[i];

8 Linear search Given an array on integers and another integer ‘x’, find if ‘x’ appears in the array. Print ‘Not found’, if ‘x’ does not appear, otherwise print ‘Found’. Strategy: Simply iterate over the array if look for a match

9 Linear search int array[100]; int i,x, found=0; …. for(i=0; i<100;i++) { if( array[i] == x) { found =1; printf(“Found %d”,x); break; } if(!found) { printf(“%d not found”, x); }

10 Linear search The worst case – the loop is executed n times On the average – the loop is executed n/2 times What if the array has a million integers (about 2 20 )

11 Binary Search Can make it faster. However, unlike linear search, binary search has a precondition The sequence must be sorted

12 Binary Search Strategy: Compare ‘x’ with the middle element. If equal, we found it Else If ‘x’ < middle element, we can discard the half of the elements; all elements that are greater than the middle element and middle element itself Now search in the remaining half of the array Similarly, if ‘x’ > middle element, we can discard the half of the elements; all elements that are smaller than the middle element and middle element itself

13 Binary Search

14 #include #define ARRSIZE 7 int main(void) { int intarr[ARRSIZE], target, i, left, right, mid; printf("Please input %d integers which are sorted in the ascending order and a target value.", ARRSIZE); scanf("%d", &target); for(i = 0; i<ARRSIZE; i++) { scanf("%d", &intarr[i]); } left = 0; right = ARRSIZE-1; while(left <= right) { mid = (left+right)/2; if(intarr[mid] == target) { printf("The index of the target in the array is %d.\n", mid); break; } else if (target > intarr[mid]) { left = mid + 1; } else { right = mid - 1; } if(left > right) printf("The target is not in the array.\n"); return 0; }

15 Binary Search How many comparisons on average? Each time ‘x’ is not found, we are taking the half of the remaining array If the array has size ‘n’, how many times we can slice it into half?

16 Assume you can compare at most ‘m’ times After 1 st half: array size n/2 After 2 nd half: array size n/4 After 2rd half: array size n/8 …. After m-th half: array size n/(2 m ) Since, the minimum array size is 1 [there has to be at least 1 element] n/(2 m ) = 1 => m = log 2 (n)

17 Performance So, in binary search, there are log 2 (n) comparisons. If you are given a sorted array of a million integers (2 20 ), Linear search can make a million comparison operations Binary search will make around 20 comparisons You will learn more on performance in algorithm classes. This is very important aspect of programming.

18 Practice problem You are given an array containing 9 integers, ranging from 1 to 10. Each number between 1 to 10 appears exactly once in the array, but unfortunately one of them is missing. Can you find the missing number?

19 Dr. Sajib Datta CSE@UTA CSE1320 Arrays and Strings

20 Sort Ordering elements in some way For numeric data, ascending order is the most common Lots of techniques for sorting These techniques vary in performance both with runtime and usage of extra memory

21 Bubble sort Given a sequence of numbers, it compares adjacent numbers and swap them if necessary  Repeats the above procedure until all numbers are in right place

22 An example 4 10 9 6 -1 Pick 4, compare with 10

23 An example 4 10 9 6 -1 Pick 10, compare with 9 Need to swap

24 An example 4 9 10 6 -1

25 An example 4 9 10 6 -1 Pick 10, compare with 6 Need to swap

26 An example 4 9 6 10 -1

27 An example 4 9 6 10 -1 Pick 10, compare with -1

28 An example 4 9 6 -1 10 Notice, that 10 is at the right position Now, repeat from the beginning

29 An example 4 9 6 -1 10 Compare 1 st element with 2 nd element

30 An example 4 9 6 -1 10 Compare 2nd element with 3rd element Swap needed

31 An example 4 6 9 -1 10

32 An example 4 6 9 -1 10 Compare 3 rd element with 4 th element Swap needed

33 An example 4 6 -1 9 10

34 An example 4 6 -1 9 10 Now, do we need the compare 4 th with the 5 th ? We already know 5 th element is the largest. This implies, what we have as the 4 th element, it must be the second largest, and is already in the correct place.

35 An example 4 6 -1 9 10 Now, repeat from the beginning again. But remember, we only need to go up to 3 rd this time.

36 What is happening inside… 1 st iteration places the largest number in the correct place 2 nd iteration places the second largest number in the correct place ….. …. i-th iteration places the i-th largest number in the correct place

37 Bubble sort #include #define ARR_SIZE 8 int main(void) { int intarr[ARR_SIZE] = {23, 4, 12, 8, 22, 1, 54, 9}, i, j, tmp; for(i = 0; i<ARR_SIZE-1; i++) { for(j = 0; j<ARR_SIZE-i-1; j++) { if(intarr[j]>intarr[j+1]) { tmp = intarr[j]; intarr[j] = intarr[j+1]; intarr[j+1] = tmp; } printf("The array sorted by Bubble Sort: "); for(i = 0; i< ARR_SIZE; i++) printf("%d ", intarr[i]); return 0; }


Download ppt "Dr. Sajib Datta CSE 1320 Arrays, Search and Sort."

Similar presentations


Ads by Google