Top Banner
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]
26

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.

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 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.

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]

Page 2: 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.

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;

Page 3: 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.

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

Page 4: 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.

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 .

Page 5: 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.

Constructing a Sort Library

• Figure 11.14 the header file sort.h for the

• programmer-defined sort library.

• #include “sort.h”.

Page 6: 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.

11.6 SEARCHING ARRAYS

• We search a list stored in an array to determine whether it contains an element that matching a given

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

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.

Page 8: 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.

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

Page 9: 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.

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

Page 10: 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.

• 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

Page 11: 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.

Constructing Search Library

• #include “search.h”

• #include <string.h>

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

11.7 EXAMPLE PROGRAM 2:

A C Program that Creates, Sort, and Searches a One-Dimensional

Array of Integers

Page 13: 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.

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 .

Page 14: 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.

Declaring Two-Dimensional Arrays

• int test_scores[4][3]; • The first subscript is the number of rows, and the second is t

he number of columns.• When the complier encounters the declaration for a two-dim

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

Page 15: 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.

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

Page 16: 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.

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.

Page 17: 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.

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]);

Page 18: 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.

• 2.in its prototype.

• void input_tale(int *no_of _rows ,

• int *no_of_columns,

• int arr[][COLUMNS_SIZE]);

Page 19: 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.

• 3. In a call

• input_table(&rows, &columns , test_scores );

Page 20: 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.

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

Page 21: 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.

• 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

Page 22: 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.

• 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 */

Page 23: 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.

• 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 */

Page 24: 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.

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

Page 25: 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.

11.9 EXAMPLE PROGRAM 3:A C Function that Generates a Ba

r Chart

Page 26: 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.