- 39 Views
- Uploaded on
- Presentation posted in: General

Fundamentals of Programming Session 17

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

Fundamentals of Programming Session 17

Fall 2013

- Instructor: Reza Entezari-Maleki
- Email:entezari@ce.sharif.edu

- These slides have beencreated using Deitel’s slides

Sharif University of Technology

- Sorting Arrays
- Searching Arrays

- Sorting data (i.e., placing the data into a particular order such as ascending or descending) is one of the most important computing applications.
- Sorting data
- Important computing application
- Virtually every organization must sort some data
- Massive amounts must be sorted

- Sorting: an operation that segregates items into groups according to specified criterion.
- A = { 3 1 6 2 1 3 4 5 9 0 }
- A = { 0 1 1 2 3 3 4 5 6 9 }

- There are many different types of sorting algorithms, but the primary ones are:
- Bubble Sort
- Selection Sort
- Insertion Sort
- Merge Sort
- Quick Sort
- Shell Sort
- Heap Sort
- Radix Sort
- Swap Sort
- ...

- Bubble sort (sinking sort)
- Several passes through the array
- Successive pairs of elements are compared
- If increasing order (or identical), no change
- If decreasing order, elements exchanged

- Repeat these steps for every element

- Example:
- Go left to right, and exchange elements as necessary
- One pass for each element

- Original: 3 4 2 7 6
- Pass 1: 3 2 46 7(elements exchanged)
- Pass 2: 2 34 6 7
- Pass 3: 2 3 4 6 7 (no changes needed)
- Pass 4: 2 3 4 6 7
- Small elements "bubble" to the top (like 2 in this example)

- Go left to right, and exchange elements as necessary

- Swapping variables
int x = 3, y = 4;

y = x;

x = y;

- What happened?
- Both x and y are 3!
- Need a temporary variable

- Solution
int x = 3, y = 4, temp = 0;

temp = x; // temp gets 3

x = y; // x gets 4

y = temp; // y gets 3

- Figure 6.15 sorts the values in the elements of the 10-element array a into ascending order.

- Selection sort
- This type of sorting is called "Selection Sort" because it works by repeatedly element.
- It works as follows:
- First find the smallest in the array and exchange it with the element in the first position,
- then find the second smallest element and exchange it with the element in the second position,
- and continue in this way until the entire array is sorted.

- Pseudocode of selection sort
SELECTION_SORT (A)

1. Fori ← 0 ton-2do2. minj ← i;3. minx ← A[i]4. Forj ← i + 1 to n do5. If A[j] < minx then6. minj ← j7. minx ← A[j]8. A[minj] ← A [i]9. A[i] ← minx

- Insertion sort algorithm somewhat resembles selection sort.
- Array is imaginary divided into two parts: sorted one and unsorted one.
- At the beginning, sorted part contains first element of the array and unsorted one contains the rest.
- At every step, algorithm takes first element in the unsorted part and inserts it to the right place of the sorted one.
- When unsorted part becomes empty, algorithm stops.

- Pseudocode of selection sort
INSERTION_SORT (A)

1. FOR j ← 1 TO length[A] 2. DO key ← A[j] 3. {Put A[j] into the sorted sequence A[1 . . j − 1]} 4. i ← j − 1 5. WHILEi > 0 and A[i] > key6. DOA[i +1] ← A[i] 7. i ← i − 1 8. A[i + 1] ← key

- It may be necessary to determine whether an array contains a value that matches a certain key value.
- The process of finding a particular element of an array is called searching.
- In this section we discuss two searching techniques—the simple linear search technique and the more efficient (but more complex) binary search technique.
- The linear search (Fig. 6.18) compares each element of the array with the search key.