Loading in 5 sec....

CSC 211 Data Structures Lecture 14PowerPoint Presentation

CSC 211 Data Structures Lecture 14

- By
**tehya** - Follow User

- 136 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about ' CSC 211 Data Structures Lecture 14' - tehya

**An Image/Link below is provided (as is) to download presentation**

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

Last Lecture Summary

- Binary Search
- Concept
- Algorithm

- Implementation of Binary search
- Complexity of Binary Search
- Comparison of Linear and Binary Search
- Searching Unordered Linked List
- Searching Ordered Linked List

2

Objectives Overview

- Sorting
- Concept

- Reasons for Sorting
- Basic Terminology
- Sorting Classification
- Stability of Key
- Bubble Sort
- Concept
- Algorithm
- Code and Implementation

Sorting

- A fundamental operation in computer science
- Task of rearranging data in an order such as
- Ascending
- Descending or
- Lexicographic

- Data may be of any type like numeric, alphabetical or alphanumeric
- It also refers to rearranging a set of records based on their key values when the records are stored in a file
- Sorting task arises more frequently in the world of data manipulation

4

Sorting

- Sorting and searching frequently apply to a file of records
- Each record in a file F can contain many fields but there may be one particular field whose values uniquely determine the records in the file called primary key
- The key or key values are k1, k2, ……
- Sorting the file F usually refers to sorting F with respect to a particular primary key
- Searching in F refers to searching for a record with a given key value

5

Sorting

- Let A be a list of n elements in memory
- A1, A2, ……., An

- Sorting refers to the operations of rearranging the contents of A so that they are increasing in order numerically or lexicographically so that
- A1 A2 A3 ………….. An

- Since A has n elements, there are n! ways that contents can appear in A
- These ways correspond precisely to the n! permutations of 1, 2, …., n
- Accordingly each sorting algorithms must take care of these n! possibilities

6

Sorting

- Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) that require sorted lists to work correctly;
- Sorting is also often useful for canonicalizing data and for producing human-readable output.
- More formally, the output must satisfy two conditions:
- The output is in non-decreasing order (each element is no smaller than the previous element according to the desired total order);
- The output is a permutation (reordering) of the input.

Reasons for Sorting

- From the programming point of view, the sorting task is important for the following reasons
- How to rearrange a given set of data?
- Which data structures are more suitable to store data prior to their sorting?
- How fast can the sorting be achieved?
- How can sorting be done in a memory constrained situation?
- How to sort various type of data?

Basic Terminology - I

- Internal sort
- When a set of data to be sorted is small enough such that the entire sorting can be performed in a computer’s internal storage (primary memory)

- External sort
- Sorting a large set of data which is stored in low speed computer’s external memory such as hard disk, magnetic tape, etc.

- Ascending order
- An arrangement of data if it satisfies “less than or equal to <=“ relation between two consecutive data
- [1, 2, 3, 4, 5, 6, 7, 8, 9]

Basic Terminology - II

- Descending order
- An arrangement of data if it satisfies “greater than or equal to >=“ relation between two consecutive data
- e.g. [ 9, 8, 7, 6, 5, 4, 3, 2, 1]

- Lexicographic order
- If the data are in the form of character or string of characters and are arranged in the same order as in dictionary
- e.g. [ada, bat, cat, mat, max, may, min]

- Collating sequence
- Ordering for a set of characters that determines whether a character is in higher, lower or same order compared to another
- e.g. alphanumeric characters are compared according to their ASCII code
- e.g. [AmaZon, amaZon, amazon, amazon1, amazon2]

Basic Terminology - III

- Random order
- If a data in a list do not follow any ordering mentioned above, then it is arranged in random order
- e.g. [8, 6, 5, 9, 3, 1, 4, 7, 2]
- [may, bat, ada, cat, mat, max, min]

- Swap
- Swap between two data storages implies the interchange of their contents.
- e.g. Before swap A[1] = 11, A[5] = 99
- After swap A[1] = 99, A[5] = 11

- Item
- Is a data or element in the list to be sorted.
- May be an integer, string of characters, a record etc.
- Also alternatively termed key, data, element etc.

Basic Terminology - IV

- Stable Sort
- A list of data may contain two or more equal data. If a sorting method maintains the same relative position of their occurrences in the sorted list then it is stable sort
- e.g. [ 2, 5, 6, 4, 3, 2, 5, 1, 5 ]
- [ 1, 2, 2, 3, 4, 5, 5, 5, 6 ]

- In Place Sort
- Suppose a set of data to be sorted is stored in an array A
- If a sorting method takes place within the array A only, i.e. without using any other extra storage space
- It is a memory efficient sorting method

Sorting Methods

- Bubble Sort Heap Sort
- Selection Sort Merge Sort
- Insertion Sort Quick Sort

Sorting Classification - I

- Sorting algorithms are often classified by:
- Computational complexity (worst, average and best behavior) of element comparisons in terms of the size of the list (n).
- For typical sorting algorithms
- good behavior is O(n log n) and
- bad behavior is O(n2).
- Ideal behavior for a sort is O(n), but this is not possible in the average case.
- Comparison-based sorting algorithms, which evaluate the elements of the list via an abstract key comparison operation, need at least O(n log n) comparisons for most inputs.

Sorting Classification - II

- Computational complexity of swaps (for "in place" algorithms).
- Memory usage (and use of other computer resources). In particular, some sorting algorithms are "in place”
- Strictly, an in place sort needs only O(1) memory beyond the items being sorted;
- sometimes O(log(n)) additional memory is considered "in place”

- Recursion. Some algorithms are either recursive or non-recursive, while others may be both (e.g., merge sort).
- Stability: stable sorting algorithms maintain the relative order of records with equal keys (i.e., values)

Sorting Classification - III

- Whether or not they are a comparison sort.
- A comparison sort examines the data only by comparing two elements with a comparison operator.

- General method: insertion, exchange, selection, merging, etc.
- Exchange sorts include bubble sort and quicksort.
- Selection sorts include shaker sort and heapsort.

- Adaptability: Whether or not the presortedness of the input affects the running time. Algorithms that take this into account are known to be adaptive.

Stability

- Stable sorting algorithms maintain the relative order of records with equalkeys.
- A key is that portion of record which is the basis for the sort
- it may or may not include all of the record

- If all keys are different then this distinction is not necessary.
- But if there are equal keys, then a sorting algorithm is stable if whenever there are two records (let's say R and S) with the same key, and R appears before S in the original list, then R will always appear before S in the sorted list.

Stability -II

- When equal elements are indistinguishable, such as with integers, or more generally, any data where the entire element is the key, stability is not an issue.
- However, assume that the following pairs of numbers are to be sorted by their first component:
- (4, 2) (3, 7) (3, 1) (5, 6)
- Two different results are possible,
- one which maintains the relative order of records with equal keys, and one which does not:

- (3, 7) (3, 1) (4, 2) (5, 6) (order maintained)
- (3, 1) (3, 7) (4, 2) (5, 6) (order changed)

Stability - III

- Unstable sorting algorithms may change the relative order of records with equal keys, but stable sorting algorithms never do so.
- Unstable sorting algorithms can be specially implemented to be stable.
- One way of doing this is to artificially extend the key comparison, so that comparisons between two objects with otherwise equal keys are decided using the order of the entries in the original data order as a tie-breaker.
- Remembering this order, however, often involves an additional computational cost.

Stability - IV

- Sorting based on a primary, secondary, tertiary, etc. sort key can be done by any sorting method, taking all sort keys into account in comparisons
- in other words, using a single composite sort key

- If a sorting method is stable, it is also possible to sort multiple times, each time with one sort key. In that case the keys need to be applied in order of increasing priority.
- Example: sorting pairs of numbers as above by second, then first component:
- (4, 2) (3, 7) (3, 1) (5, 6) (original)
- (3, 1) (4, 2) (5, 6) (3, 7)
- after sorting by second component

- (3, 1) (3, 7) (4, 2) (5, 6)
- after sorting by first component

Stability - V

- On the other hand:
- (4, 2) (3, 7) (3, 1) (5, 6) (original)
- (3, 7) (3, 1) (4, 2) (5, 6)
- after sorting by first component

- (3, 1) (4, 2) (5, 6) (3, 7)
- after sorting by second component
- order by first component is disrupted

Bubble Sort

- Sometimes incorrectly referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order.
- The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.
- The algorithm gets its name from the way smaller elements "bubble" to the top of the list.
- Because it only uses comparisons to operate on elements, it is a comparison sort.

Bubble Sort

- Bubble sort is a simple sorting algorithm
- The algorithm starts at the beginning of the data set.
- It compares the first two elements, and if the first is greater than the second, it swaps them.
- It continues doing this for each pair of adjacent elements to the end of the data set.
- It then starts again with the first two elements, repeating until no swaps have occurred on the last pass.

- Note that the largest end gets sorted first, with smaller elements taking longer to move to their correct positions.

Bubble Sort Algorithm

- Suppose the list of numbers A[1], A[2], …. A[N] is in memory. The bubble sort algorithm works as follows:
- Step 1: Compare A[1] and A[2] and arrange them in the desired order, so that A[1] < A[2].
- Then compare A[2] and A[3] and arrange them so that A[2] < A[3]]. Then compare A[3] and A[4] and arrange them so that A[3] < A[4]. Continue until we compare A[N – 1] with A[N] and arrange them so that A[N – 1] < A[N].
- Observe that Step 1 involves n – 1 comparisons. During Step 1, the largest element is “bubbled up”to the nth position or “sinks”to the nth position.
- When Step 1 is completed, A[N] will contain the largest element

Bubble Sort Algorithm

- Step 2: Repeat Step 1 with one less comparison . i.e. now we stop after we compare and possible rearrange A[N - 2] and A[N - 1].
- Step 2 involves N – 2 comparisons and when Step 2 is completed, A[N - 1] will contain the second largest element.
- Step 3: Repeat Step 1 with two fewer comparisons . i.e. we stop after we compare and possible rearrange A[N - 3] and A[N - 2]
- Step 3 involves N – 3 comparisons and when Step 2 is completed, A[N - 1] will contain the third largest element

Bubble Sort Algorithm

- ………………………………………………….
- ………………………………………………….
- Step N - 1: Compare A[1] with A[2] and arrange them so that A[1] < A[2].
- After n -1 steps the list will be sorted in the ascending order
- The process of sequentially traversing through all or part of a list is called a “pass” so each of the above step is called a pass
- Accordingly, the bubble sort algorithm requires n – 1 passes where n is the number of input items

Bubble Sort Step-by-Step Example

- Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort
- In each step, elements written in bold are being compared
- Three passes will be required
- First Pass:( 51 4 2 8 ) ( 154 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.( 1 54 2 8 ) ( 1 45 2 8 ), Swap since 5 > 4( 1 4 52 8 ) ( 1 4 25 8 ), Swap since 5 > 2( 1 4 2 58 ) ( 1 4 2 58 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.

Bubble Sort Step-by-Step Example

- Second Pass:( 14 2 5 8 ) ( 142 5 8 )( 1 425 8 ) ( 1 24 5 8 ), Swap since 4 > 2( 1 2 45 8 ) ( 1 2 45 8 )( 1 2 4 58 ) ( 1 2 4 58 )Now, the array is already sorted, but our algorithm does not know if it is completed
- The algorithm needs one wholepass without any swap to know it is sorted
- Third Pass:
( 124 5 8 ) ( 12 4 5 8 )( 1 24 5 8 ) ( 1 24 5 8 )( 1 2 45 8 ) ( 1 2 45 8 )( 1 2 4 58 ) ( 1 2 4 58 )

The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

Bubble Sort Algorithm[0][1] [2] [3] [4] [5]

The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

Bubble Sort AlgorithmSwap?

[0][1] [2] [3] [4] [5]

The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

Bubble Sort AlgorithmYes!

[0][1] [2] [3] [4] [5]

The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

Bubble Sort AlgorithmSwap?

[0][1] [2] [3] [4] [5]

The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

Bubble Sort AlgorithmNo.

[0][1] [2] [3] [4] [5]

The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

Bubble Sort AlgorithmSwap?

[0][1] [2] [3] [4] [5]

The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

Bubble Sort AlgorithmNo.

[0][1] [2] [3] [4] [5]

The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

Bubble Sort AlgorithmSwap?

[0][1] [2] [3] [4] [5]

The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

Bubble Sort AlgorithmYes!

[0][1] [2] [3] [4] [5]

The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

Bubble Sort AlgorithmSwap?

[0][1] [2] [3] [4] [5]

The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

Bubble Sort AlgorithmYes!

[0][1] [2] [3] [4] [5]

Repeat. array, and swaps their order if needed.

Bubble Sort AlgorithmSwap? No.

[0][1] [2] [3] [4] [5]

Repeat. array, and swaps their order if needed.

Bubble Sort AlgorithmSwap? No.

[0][1] [2] [3] [4] [5]

Repeat. array, and swaps their order if needed.

Bubble Sort AlgorithmSwap? Yes.

[0][1] [2] [3] [4] [5]

Repeat. array, and swaps their order if needed.

Bubble Sort AlgorithmSwap? Yes.

[0][1] [2] [3] [4] [5]

Repeat. array, and swaps their order if needed.

Bubble Sort AlgorithmSwap? Yes.

[0][1] [2] [3] [4] [5]

Repeat. array, and swaps their order if needed.

Bubble Sort AlgorithmSwap? Yes.

[0][1] [2] [3] [4] [5]

Loop over array n-1 times, swapping pairs of entries as needed.

Bubble Sort AlgorithmSwap? No.

[0][1] [2] [3] [4] [5]

Loop over array n-1 times, swapping pairs of entries as needed.

Bubble Sort AlgorithmSwap? Yes.

[0][1] [2] [3] [4] [5]

Loop over array n-1 times, swapping pairs of entries as needed.

Bubble Sort AlgorithmSwap? Yes.

[0][1] [2] [3] [4] [5]

Loop over array n-1 times, swapping pairs of entries as needed.

Bubble Sort AlgorithmSwap? Yes.

[0][1] [2] [3] [4] [5]

Loop over array n-1 times, swapping pairs of entries as needed.

Bubble Sort AlgorithmSwap? Yes.

[0][1] [2] [3] [4] [5]

Bubble Sort Algorithm - Result needed.

[0][1] [2] [3] [4] [5]

Bubble Sort Algorithm needed.

procedure bubbleSort( A : list of sortable items )

n = length (A)

repeat

swapped = false

for i = 1 to n - 1 inclusive do:

/* if this pair is out of order */

if A[i-1] > A[i] then

/* swap them and remember something changed */

swap( A[i-1], A[i] )

swapped = true

end if

end for

until not swapped

end procedure

Bubble Sort Algorithm - Optimized needed.

- Can be easily optimized by observing that the n-th pass finds the n-th largest element and puts it into its final place
- So, the inner loop can avoid looking at the last n-1 items when running for the n-th time:
procedure bubbleSort( A : list of sortable items )

n = length(A)

repeat

swapped = false

for i = 1 to n-1 inclusive do

if A[i-1] > A[i] then

swap(A[i-1], A[i])

swapped = true

end if

end for

n = n - 1

until not swapped

end procedure

Bubble Sort Algorithm – Optimized 2 needed.

- More generally, it can happen that more than one element is placed in their final position on a single pass.
- In particular, after every pass, all elements after the last swap are sorted, and do not need to be checked again. This allows us to skip over a lot of the elements, resulting in about a worst case 50% improvement in comparison count (though no improvement in swap counts), and adds very little complexity because the new code subsumes the "swapped" variable:
procedure bubbleSort( A : list of sortable items )

n = length(A)

repeat

newn = 0

for i = 1 to n-1 inclusive do

if A[i-1] > A[i] then

swap(A[i-1], A[i])

newn = i

end if

end for

n = newn

until n = 0

end procedure

Bubble Sort needed. Implementation Code

void bubbleSort (intlist[ ] , int size) {

inti, j, temp;

for ( i = 0; i < size; i++ ) { /* controls passes through the list */

for ( j = 0; j < size - 1; j++ ) /* performs adjacent comparisons */

{

if ( list[ j ] > list[ j+1 ] ) /* determines if a swap should occur */

{

temp = list[ j ]; /* swap is performed */

list[ j ] = list[ j + 1 ];

list[ j+1 ] = temp;

} // end of if statement

} // end of inner for loop

} // end of outer for loop

} // end of function

Bubble needed. Sort Using Call-by-reference

- Implement bubblesort using pointers
- Swap two elements
- swap function must receive address (using &) of array elements
- Array elements have call-by-value default

- Using pointers and the * operator, swap can switch array elements

- Psuedocode
Initialize array

print data in original order

Call function bubblesort

print sorted array

Define bubblesort and Swap functions

1 needed. /* Fig. 7.15: fig07_15.c

2 This program puts values into an array, sorts the values into

Bubblesort gets passed the address of array elements (pointers). The name of an array is a pointer.

3 ascending order, and prints the resulting array. */

4 #include <stdio.h>

5 #define SIZE 10

6 void bubbleSort( int *, constint );

7

8 int main()

9 {

10

11 int a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };

12 int i;

13

14 printf( "Data items in original order\n" );

15

16 for ( i = 0; i < SIZE; i++ )

17 printf( "%4d", a[ i ] );

18

19 bubbleSort( a, SIZE ); /* sort the array */

20 printf( "\nData items in ascending order\n" );

21

22 for ( i = 0; i < SIZE; i++ )

23 printf( "%4d", a[ i ] );

24

25 printf( "\n" );

26

27 return 0;

28 }

29

1. Initialize array

1.1 Declare variables

2. Print array

2.1 Call bubbleSort

2.2 Print array

33 needed. int pass, j;

34 for ( pass = 0; pass < size - 1; pass++ )

35

36 for ( j = 0; j < size - 1; j++ )

37

38 if ( array[ j ] > array[ j + 1 ] )

39 swap( &array[ j ], &array[ j + 1 ] );

40 }

41

42 void swap( int *element1Ptr, int *element2Ptr )

43 {

44 int hold = *element1Ptr;

45 *element1Ptr = *element2Ptr;

46 *element2Ptr = hold;

47 }

30 voidbubbleSort(int*array, constintsize)

31{

32void swap( int *, int * );

3. Function definitions

Program Output

Data items in original order

2 6 4 8 10 12 89 68 45 37

Data items in ascending order

2 4 6 8 10 12 37 45 68 89

Bubble Sort needed.

Summary needed.

- Sorting
- Concept

- Reasons for Sorting
- Basic Terminology
- Sorting Classification
- Stability of Key
- Bubble Sort
- Concept
- Algorithm
- Code and Implementation

Download Presentation

Connecting to Server..