Download presentation
Presentation is loading. Please wait.
Published byLynn Blair Modified over 9 years ago
1
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 2
2
Definition of Arrays Operators for Arrays Value Assignment Insertion Search Deletion Ordered Arrays Search Algorithms ▪ Binary Search ▪ Interpolation Search Sort Algorithms ▪ Bubble Sort ▪ Insertion Sort ▪ Quick Sort
3
An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. A[0] …… A[1]A[2]A[3]A[4]A[5]A[6]A[7]
4
Memory storage for 2 Dimensional arrays A[0][0] A[4][2] 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 A[0][0] A[0][1] A[0][2] A[0][3] A[0][4] A[0][5] A[0][6] A[0][7] A[1][0] A[1][1] A[1][2] A[1][3] ……
5
Can represent 3 Dimensional elements A[0][0][0] A[1][2][0] A[2][3][1]
6
Value Assignment Insertion Search Deletion
7
Just give the value to the corresponding array element Example: Assign the value 21 as the 2 nd element ▪ A[1]=21; A[0] …… A[1]A[2]A[3]A[4]A[5]A[6]A[7] 21
8
Steps: 1. Insert the value to the corresponding position 2. Move all the items with higher index values up one position Example: Insert the value 21 as the 2 nd element ▪ A[1] <- 21; A[0] …… A[1]A[2]A[3]A[4]A[5]A[6]A[7] 21 A[0] …… A[2]A[3]A[4]A[5]A[6]A[7]A[8] 21
9
The searchKey variable holds the value we’re looking for. To search for an item, step through the array, comparing searchKey with each element. Example Search value 27 Search value 23 3222711131448 A[i] i=0 i=1 i=2 Output: Found in i=2 A[k] k=0k=1k=2k=3k=4k=5k=6k=7 Output: Can’t find 23
10
Steps: 1. Begin with a search 2. Delete the target element 3. move all the items with higher index values down one element to fill in the “hole” left by the deleted element Example: Delete the first value 21 31411171521 4361 i=0i=1i=2i=3i=4i=5
11
The data in ordered arrays is stored in ascending (or descending) key order Benefit: Possible faster way of searching Shortcoming: Need to be sorted 73251547681 8595 Value is increasing
12
Solution of The Guess-a-Number Game Example: Search value 62 71231495662859197 i=4i=6i=5 62>5662<85 Output: Found in i=5
13
Motivated by the phonebook search In previous example 1 st Round 71231495662859197 A[6-1]=62 Found!
14
Sort as bubble arising Example: Sort by increasing Length Swap } Sorted How many iterations are needed for complete sort?
15
The simplest sort algorithm Easy to understand Easy to be implemented The least efficient Two Level Loops Computational Complexity: i from 1 to N-1 step 1 j from 0 to N-i { Swap( j, j+1 ); }
16
Divide the array into two parts Sorted part Unsorted part Insert the elements in the unsorted part into the sorted part in terms of the key value Sorted PartUnsorted Part
17
Computational Complexity: some can be substantially faster than others. Insertion sort is stable Require a single temporary variable in addition to the original array
18
A Divide-and-Conquer solution 1. Divide the array into two parts by a selected pivot value α ▪ the elements smaller than α in left part ▪ Otherwise in right part 2. Sort the two parts individually as two independent arrays by Quick Sort
19
Select as the Pivot SmallerLarger Pivot
20
How to select the pivot?
21
Partitioning operates in linear O(N) time, making N plus 1 or 2 comparisons and fewer than N/2 swaps. The partitioning algorithm may require extra tests in its inner while loops to prevent the indices running off the ends of the array. The pivot value for a partition in Quick Sort is the key value of a specific item, called the pivot.
22
Definition of Arrays Operators for Arrays Insertion Search Deletion Ordered Arrays Search Algorithms ▪ Binary Search ▪ Interpolation Search Sort Algorithms ▪ Bubble Sort ▪ Insertion Sort ▪ Quick Sort
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.