Presentation is loading. Please wait.

Presentation is loading. Please wait.

11.5 SORTING ARRAY Sorting is the process of transforming a list into an equivalent list, in which the elements are arranged in ascending or descending.

Similar presentations


Presentation on theme: "11.5 SORTING ARRAY Sorting is the process of transforming a list into an equivalent list, in which the elements are arranged in ascending or descending."— Presentation transcript:

1 11.5 SORTING ARRAY Sorting is the process of transforming a list into an equivalent list, in which the elements are arranged in ascending or descending order. A sorting list is called an ordered list. arr[0]<= arr[1] <= ….,<= arr[n-2]<=arr[n-1]

2 Bubble Sort Comparing two consecutive array elements and swapping them if the first is greater than the second. At the end of the first pass,we have succeeded in pushing the largest value in the array to its end. Fig 11.9 A C function that sorts an array using bubble sort. typedef int list_item_type;

3 Selecting Sort Of selecting the largest array element and swapping it with the last array element. Figure 11.11 A C function that sorts an array using selection sort

4 Insertion Sort Of inserting a new item into a list of ordered items. Figure 11.13 A C function that sorts an array using insertion sort.

5 Constructing a Sort Library Figure 11.14 the header file sort.h for the programmer-defined sort library. #include “sort.h”.

6 11.6 SEARCHING ARRAYS We search a list stored in an array to determine whether it contains an element that matching a given

7 Sequential Search We access list elements, starting with the first,and compare each element with the search key. If we find a match, the search is successful. If we access and compare the last list element and still have no match, then the search is unsuccessful.

8 Improve the efficiency of this algorithm by running it on ordered list. Algorithm terminates if the search key value turns out to be less than an array element.

9 Binary Search Compares the search key value with the value of the list element that is midway in the list. Figure 11.20 A C function that searches an array using binary search

10 Figure 11.21 Comparing efficiencies of sequential and binary search List_size sequential search binary search 100 100 7 1,000,000 1,000,000 20 1,000,000,000 1,000,000,000 30

11 Constructing Search Library #include “search.h” #include

12 11.7 EXAMPLE PROGRAM 2: A C Program that Creates, Sort, and Searches a One-Dimensional Array of Integers

13 11.8 HIGHT –DIMENSIONAL ARRAYS An array of one-dimensional arrays is called a two-dimensional arrays; An array of two-dimensional arrays is called a three-dimensional array,and so on. A two-dimensional arrays is equivalent to a table of a fixed number of rows and a fixed number of columns.

14 Declaring Two-Dimensional Arrays int test_scores[4][3]; The first subscript is the number of rows, and the second is the number of columns. When the complier encounters the declaration for a two- dimensional array,it allocates memory locations for its elements in a linear fashion. The memory locations for the elements on row 0 are followed by the memory locations for the elements on row 1;

15 Initialization of Two- Dimensional arrays int test_scores[4][3]= {95,80,78,69,75,81,100,98,100,98,85,87}; int test_scores[][3]= {{95,80,78},{69,75,81},{100,98,100},{98, 85,87}};

16 Operations on Two–Dimensional Arrays If we declare a two-dimensional array in the formal parameter list as int b [][], Each element of the one-dimensional array is a one-dimensional array. We must also tell the complier about the number of elements in each row of the two-dimensional array. Therefore in the formal parameter list the proper declaration must be as int b[][20],thus enabling the complier to compute the offset for the second row as 2*20=40.

17 The rules for passing two- dimensional arrays to functions 1. Be declared void input_tale(int *no_of _rows, int *no_of_columns, int arr[][COLUMNS_SIZE]);

18 2.in its prototype. void input_tale(int *no_of _rows, int *no_of_columns, int arr[][COLUMNS_SIZE]);

19 3. In a call input_table(&rows, &columns, test_scores );

20 Two-Dimensional Arrays and Pointers int t[4][3] t as a pointer to row 0. *t a pointer to t[0][0]. We can access its content by again applying the indirection operator as *(*t). it is *t+1,and the reference to t[0][1] in pointer/offset notion is *(*t+1).

21 The pointer to row 1 is t+1. For example, the pointer to the third element in row 1 is *(t + 1 ) +2,and the name of that element is *(*(t + 1) +2). *(*( t + i ) + j) is the name of the array element t[i][j] in pointer/offset notation

22 for (i=0; i<4; i++) { printf (“\nROW %d OF array t: “, i); for (j=0 ; j<3 ; j++) printf (“%d”, t[i][j]); /*end for */ } /* end for */

23 for (i=0 ; i < 4 ; i++ ) { printf (“\nROW %d OF array t: “, i); for (j=0 ; j<3 ; j++) printf (“%d”, *(* t + i ) + j ); /*end for */ } /* end for */

24 Suggest that you avoid using pointer/offset notation unless program efficiency for compilation turns out to be very important in you application.

25 11.9 EXAMPLE PROGRAM 3: A C Function that Generates a Bar Chart

26


Download ppt "11.5 SORTING ARRAY Sorting is the process of transforming a list into an equivalent list, in which the elements are arranged in ascending or descending."

Similar presentations


Ads by Google