1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the...

53
1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field Examples: Student Id, Social Security Number, employee ID Random order: records are in the order in which they were added Sorting: placing the records in order, based on the values in one or more fields Ascending order: arranged from lowest to highest Descending order: arranged from highest to lowest

Transcript of 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the...

Page 1: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

1

Understanding the Need for Sorting Records

• Sequential order: records are arranged based on the value in a field– Examples: Student Id, Social Security Number,

employee ID• Random order: records are in the order in which

they were added• Sorting: placing the records in order, based on the

values in one or more fields• Ascending order: arranged from lowest to highest• Descending order: arranged from highest to lowest

Page 2: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

2

Understanding the Need for Sorting Records (continued)

• Median value: the middle item when values are listed in order

• Mean value: arithmetic average

• Computer always sorts based on numeric values– Character data is sorted by its numeric code value– “A” is less than “B”– Whether “A” is greater than “a” is system dependent

Page 3: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

3

Understanding How to Swap Two Values

• Swapping two values central to most sorting techniques

• When swapping two variables, reverse their position

• Use a temporary variable to hold one value

Page 4: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

4

Figure 9-1 Program segment that swaps two values

Page 5: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

Swap to numberint main(){

int No1,No2,temp;No1=6;No2=10;temp=No1;No1=No2;No2=temp;printf(“No1=%d”,No1);printf(“No2=%d”,No2);return 0;

}

5

Page 6: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

6

Using a Bubble Sort

• Bubble sort: one of the simplest sorting techniques• Items in a list compared in pairs• If an item is out of order, it swaps places with the

item below it• In an ascending sort, after a complete pass through

the list:– Largest item “sunk” to the bottom – Smallest item “bubbled” to the top

Page 7: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

7

Figure 9-2 The SortScores program

Page 8: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

Program to sort test score

int main()

{

const int SIZE=5;

int score[SIZE];

fillArray(score,SIZE);

sortArray(score,SIZE);

displayArray(score,SIZE);

return 0;

}

8

Page 9: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

9

Figure 9-3 The fillArray() method

Page 10: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

fillArray() method

void fillArray(int score[],int size)

{

int i=0;

while(i<size)

{

printf(“Enter score”);

scanf(“%d”,&score[i];

i=i+1;

}

}10

Page 11: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

11

Figure 9-4 The incomplete sortArray() method

Page 12: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

Incomplete sortArray() functionvoid sortArray(int score[],int SIZE)

{

int x=0;

int comps=size-1;

while(x<comps)

{

if(score[x]>score[x+1])

swap(score,x);

x=x+1;

}

}12

Page 13: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

13

Figure 9-5 The swap() method

Page 14: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

Swap() method

void swap(int score[],int x)

{

int temp;

temp=score[x+1];

score[x+1]=score[x];

score[x]=temp;

}

14

Page 15: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

15

Using a Bubble Sort (continued)• Start with x = 0, compare first pair and swap

score[0] = 90score[1] = 85score[2] = 65score[3] = 95score[4] = 75

score[0] = 85score[1] = 90score[2] = 65score[3] = 95score[4] = 75

• x = 1, compare with next and swap

score[0] = 85score[1] = 90score[2] = 65score[3] = 95score[4] = 75

score[0] = 85score[1] = 65score[2] = 90score[3] = 95score[4] = 75

Page 16: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

16

Using a Bubble Sort (continued)• With x = 2, no swap needed

score[0] = 85score[1] = 65score[2] = 90score[3] = 95score[4] = 75

score[0] = 85score[1] = 65score[2] = 90score[3] = 95score[4] = 75

• x = 3, compare with x = 4 and swap

score[0] = 85score[1] = 65score[2] = 90score[3] = 95score[4] = 75

score[0] = 85score[1] = 65score[2] = 90score[3] = 75score[4] = 95

Page 17: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

17

Figure 9-6 The completed sortArray() method

Page 18: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

The complete sortArray()void sortArray(int score[],int SIZE){

int x=0,y=0;int comps=size-1;while(y<comps){

x=0;while(x<comps){

if(score[x]>score[x+1])swap(score,x);

x=x+1;}y=y+1;

}}

18

Page 19: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

19

Using a Bubble Sort (continued)

• Use nested loops for sorting an array

• Inner loop makes the pair comparisons

• Greatest number of comparisons is one less than the number of array elements

• Outer loop controls the number of times to process the list– One less than the number of array elements

Page 20: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

20

Figure 9-6 The displayArray() method

Page 21: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

displayArray()void displayArray(int score[],int SIZE)

{

int x=0;

while(x<SIZE);

{

printf(“%d”,score[x]);

x=x+1;

}

}

21

Page 22: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

22

Sorting a List of Variable Size

• Use a variable to hold the number of array elements• Declare the array with a large fixed size• Count the number of input elements• Store count in variable• Use the variable to determine size of array

Page 23: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

23

Refining the Bubble Sort by Reducing Unnecessary Comparisons

• After the first pass through the array:– Largest item must be at the end of array– Second largest item must be at the second-to-last

position in the array

• Not necessary to compare those two values again

• On each subsequent pass through the array, stop the pair comparisons one element sooner

Page 24: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

24

Figure 9-9 Flowchart and pseudocode for sortArray() method using pairsToCompare variable

Page 25: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

25

Refining the Bubble Sort by Eliminating Unnecessary Passes

• Need one fewer pass than the number of elements to completely sort the array

• Can reduce the number of passes if array is somewhat ordered already

• Use a flag variable to indicate if there were any swaps during a single pass

• If no swaps, the array is completely sorted

Page 26: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

26Figure 9-10 Flowchart and pseudocode for sortArray() method using switchOccurred variable

Page 27: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

27Figure 9-10 Flowchart and pseudocode for sortArray() method using switchOccurred variable (continued)

Page 28: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

28

Using an Insertion Sort

• Bubble sort is one of the least efficient sorting methods

• Insertion sort requires fewer comparisons• Compare a pair of elements• If an element is smaller than the previous one, search

the array backward from that point– Insert this element at the proper location

Page 29: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

29

Figure 9-11 Movement of the value 75 to a “better” array position in an insertion point

Page 30: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

30

Figure 9-12 Insertion sort

Page 31: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

31

Using a Selection Sort

• Selection sort: two variables store the smallest value and its position in the array

• Store first element value and its position in variables• Compare it to the next element

– If next element is smaller, put its value and position in the variables

• Continue until end of array, at which time the smallest value and its position are in the variables

• Swap the first element value and position with the element and position stored in the variables

Page 32: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

32

Using a Selection Sort (continued)

• Start at the second element in the array and repeat the process

• Continue until all elements except the last have been designated as the starting point

• After making one fewer pass than the number of elements, the array is sorted

Page 33: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

33

Figure 9-13 A selection sort method

Page 34: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

34

Figure 9-13 A selection sort method (continued)

Page 35: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

35

Using Multidimensional Arrays

• One-dimensional (single-dimensional) array– Represents a single list of values

• Multidimensional array – A list with two or more related values in each position

• Two-dimensional array– Represents values in a table or grid containing rows

and columns– Requires two subscripts

• Three-dimensional array– Supported by many programming languages– Requires three subscripts

Page 36: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

36

Figure 9-14 View of a single-dimensional array in memory

Page 37: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

37

Table 9-1 Rent schedule based on floor and number of bedrooms

Page 38: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

38

Figure 9-15 Two-dimensional rent array based on rent schedule in Table 9-1

Page 39: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

39

Figure 9-16 A program that determines rents

Page 40: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

40

Using a Built-In ARRAY Class

• Similar tasks frequently performed on different arrays– Example: filling and sorting

• Modern programming languages provide an Array class

• Newer languages have vast libraries– Contain built-in methods

• Most useful Array class contains overloaded versions of each method call for each data type– Different versions of sort() for numeric and string

elements• If no Array class available, write your own

Page 41: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

41

Table 9-2 Typical useful methods of the Array class

Page 42: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

42

Using Indexed Files

• Large data file to be accessed in sorted order; usually one field determines the sorting

• Key field: field whose contents make the record unique

• Indexing records: list of key fields paired with corresponding file position

• Sorting indexes faster than physically sorting actual records

• Random-access storage device: records accessed in any order

Page 43: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

43

Table 9-3 Sample index

Page 44: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

44

Using Indexed Files (continued)

• Address: location within computer memory or storage

• Every data record on disk has an address

• Index: holds physical addresses and key field values

• Data file in physical order– Index sorted in logical order

• When a record is removed from an indexed file, deleted from the index file– Not physically removed

Page 45: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

45

Using Linked Lists

• Linked list: requires one extra field in every record– Holds the physical address of next logical record

• Add a new record:– Search the linked list for the correct logical location– Insert the new record

• Link previous record to new record• Link new record to next record

• Delete a record by unlinking it• More sophisticated linked lists store a next and a

previous field– Traversed both forward and backward

Page 46: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

46

Table 9-4 Sample linked customer list

Page 47: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

47

Table 9-5 Updated customer list

Page 48: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

48

Summary

• Sort data records based on the contents of one or more fields– Ascending order– Descending order

• Swap two values– Temporary variable holds one of the values

• Bubble sort – Compares pairs– Swaps with item below– Ascending bubble sort, largest item sinks to bottom

• Smallest item rises to the top

Page 49: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

49

Summary (continued)

• Bubble sort (continued)– Eliminate unnecessary comparisons in each pass and

eliminate unnecessary passes to improve bubble sort– Size of list to be sorted may vary

• Count values to be sorted

• Initialize array with count variable when value is known

– Bubble sort improved by stopping comparisons one element sooner on each pass

– Bubble sort improved by stopping when all items sorted• Flag variable indicates when any item is swapped

– Indicates when no items swapped in one pass through

Page 50: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

50

Summary (continued)

• Insertion sort usually requires fewer comparisons– Pairwise comparisons– Locate out-of-order element

• Search array backward to a smaller element• Move each element down one• Insert out-of-order element into open position

Page 51: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

51

Summary (continued)

• Ascending selection sort– First element assumed to be smallest

• Value and position stored in variables– Every subsequent element tested

• Smallest element found, value and position saved– Lowest value in first position after one pass through

array

Page 52: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

52

Summary (continued)

• One-dimensional array– Also called single-dimensional array– Single column of values– Accessed with a single subscript

• Most object-oriented languages support two-dimensional arrays– Both rows and columns– Two subscripts

Page 53: 1 Understanding the Need for Sorting Records Sequential order: records are arranged based on the value in a field –Examples: Student Id, Social Security.

53

Summary (continued)

• Array class contains useful methods for manipulating arrays

• Indexed files access data records in a logical order– Differs from physical order– Must identify key field for each record

• Linked list contains extra field within every record– Extra field holds physical address of next logical

record