Searching & Sorting

28
C# PROGRAMMING Searching & Sorting

description

Searching & Sorting. C# Programming . Objective/Essential Standard. Essential Standard 3.00 Apply Advanced P roperties of Arrays Indicator 3.03 Apply procedures to sort and search arrays. (6%). Searching vs. Sorting. - PowerPoint PPT Presentation

Transcript of Searching & Sorting

Page 1: Searching & Sorting

C# PROGRAMMING

Searching & Sorting

Page 2: Searching & Sorting

Objective/Essential Standard

Essential Standard 3.00 Apply Advanced Properties of Arrays

Indicator 3.03 Apply procedures to sort and search arrays. (6%)

Page 3: Searching & Sorting

Searching vs. Sorting

When you search an array, it is to determine if a specified value is present. There are two primary searching algorithms

1. Linear Search2. Binary Search

Sorting is done to order the values in the array based upon some key value. There are three primary sorting algorithms

1. Selection Sort2. Insertion Sort3. Merge Sort

Page 4: Searching & Sorting

C# PROGRAMMING

Searching Algorithms

Page 5: Searching & Sorting

Linear Search

A linear search algorithm searches the array in a sequential manner.

The algorithm moves through the array, comparing the key value with the values of the elements. If it does not find the key value, it simply moves to the next element.

5 8 6 1 2 7 3intArray

0 1 2 3 4 5 6

Page 6: Searching & Sorting

Linear Search

for (int index = 0; index < intArray.Length; index++){ if (array [index] == searchValue) return index;

return -1;}

Note: The Loop will iterate until the value is found or it reaches the end.

Page 7: Searching & Sorting

Binary Search

The binary search algorithm is more efficient than the linear search algorithm.

Binary search requires that the array be sorted first.

The algorithm splits the array and checks the middle value. If it is not found it compares the values. If the search value is higher than the middle value, the algorithm

moves to the upper half (now a subarray). (Lower – it moves to the lower half.

It splits the subarray in half, checks the middle for a match. If not found, it checks to see if it is higher/lower and moves to

appropriate subarray. This continues until it has no more values or finds a match.

Page 8: Searching & Sorting

Binary SearchBinary Search Code

int low = 0; //starts with beginning position at 0

int high = arr.Length – 1; //starts with end position at last elementint mid = (low + high + 1)/2; //calculates middle positionint index = -1; //sets index to -1, not found at

beginningdo{

if (searchVal == arr[mid]) //checks middle index = mid;else if (searchVal < arr[mid]) //sets subarray if searchVal < mid high = mid – 1;else if (searchVal > arr[mid]) //sets subarray if searchVal > mid low = mid + 1;

mid = (low + high + 1)/2; //recalculate middle value} while ((low <= high) && (index ==-1)) //continues loop while not found

return index;

Page 9: Searching & Sorting

C# PROGRAMMING

Sorting Algorithms

Page 10: Searching & Sorting

Selection Sort

This is a simple sorting algorithm.

It moves through the array looking for the lowest value, then moves it to the front.

The second pass through the array, it looks for the second lowest and moves it to the second position.

It keeps passing through the array until the last iteration.

Page 11: Searching & Sorting

Selection Sort

5 4 8 1 3

1 4 8 5 3

1 3 8 5 4

1 3 4 5 8

1 3 4 5 8

Page 12: Searching & Sorting

Selection Sort

int smallestVal;

for ( int i = 0; i < arrName.Length – 1; i++){ smallestVal = i;

for ( int index = i + 1; index < arrName.Length; index ++){

if ( arrName [ index ] < arrName [ smallestVal ])smallestVal = index;

int temp = arrName [ i ];data [ i ] = data [ smallestVal ];data [ smallestVal ] = temp;

}}

Page 13: Searching & Sorting

Insertion Sort

Another simple sorting algorithm.

1st Iteration – Compares element 1 & 2 – Swaps if element 1 > element 2.

2nd Iteration – Looks at element 3 – Inserts it in position given element 1 & 2.

It keeps comparing and inserting until the end.

Page 14: Searching & Sorting

1 4 3 5 8

1 4 5 3 8

1 4 5 8 3

4 1 5 8 3

4 5 1 8 3

4 5 8 1 3

Insertion Sort5 4 8 1 3

4 5 8 1 3

1 3 4 5 8

1 3 4 5 8

Page 15: Searching & Sorting

Insertion Sort

arr = new int [size];int insertVal;

for(int next = 1; next < arr.Length; next ++){insertVal = arr[next]; //hold valueint move = next; //initialize index pos to put elementwhile (move > 0 && arr [move – 1] > insert){

arr [move ] = arr [move – 1];move --;

}arr [move] = insertVal; //insert value

}

Page 16: Searching & Sorting

Merge Sort

The merge sort algorithm sorts the array by splitting the array into two subarrays, sorting the subarrays, then merging them back together sorted.56 18 65 17 35 29 44

56 18 65 17 35 29 44

56 18 65 17 35 29 44

56 18 65 4417 35 29

18 56 17 65 29 35 44

17 18 56 65 29 35 44

17 18 29 35 44 56 65

Page 17: Searching & Sorting

Measuring Efficiency

How efficiency an algorithm is can be measured using Big-O notation.

Big-O notation measure the worst-case runtime for an algorithm.

Page 18: Searching & Sorting

Array Class

There are methods in the Array class that can be used to search and sort an array.

You can sort an array in ascending order as follows

Array.Sort(arrName);

You can sort an array in descending order as well.

Array.Reverse(arrName);

Page 19: Searching & Sorting

Array Class

You can also use the BinarySearch method to perform a binary search. Remember the array has to be sorted first. This method returns the index position of the found

item, if not found, -1.

Example

intPosition = Array.BinarySearch(arrName, search);

Page 20: Searching & Sorting

ArrayList Class

There are methods in the ArrayList class that can be used to search and sort an ArrayList.

You can sort an array in ascending order as follows

ArrListName.Sort();

You can sort an ArrayList in descending order as well.

ArrListName.Reverse();

Page 21: Searching & Sorting

ArrayList Class

You can also use the BinarySearch method to perform a binary search with an ArrayList. Remember the ArrayList has to be sorted first. This method returns the index position of the found

item, if not found, -1.

Example

intIndex = ArrListName.BinarySearch(searchObj);

Page 22: Searching & Sorting

Check Your Understanding

Given the following code, what will be the content of the array after execution? ArrayList Treasure = new ArrayList(); HealthItem Potion= new HealthItem (5); HealthItem Life= new HealthItem (10); chest.Add(Potion); chest.Add(Potion); chest.Add(Life); chest.Remove(Potion); chest.Reverse();

Potion, Life

Page 23: Searching & Sorting

Check Your Understanding

Given the following code, what will be the value of Location after execution? ArrayList Treasure = new ArrayList(); HealthItem Potion= new HealthItem (5); HealthItem Life= new HealthItem (10); Treasure.Add(Potion); Treasure.Add(Potion); Treasure.Add(Life); Treasure.Remove(Potion);

int Location = Treasure.BinarySearch(Life);Location = 1

Page 24: Searching & Sorting

Check Your Understanding

Given the following code, what will be the value of Location after execution? ArrayList Treasure = new ArrayList(); HealthItem Potion= new HealthItem (5); HealthItem Life= new HealthItem (10); Treasure.Add(Potion); Treasure.Add(Potion); Treasure.Add(Life); Treasure.Remove(Potion);

int Location = Treasure.BinarySearch(Heart);Location = -1

Page 25: Searching & Sorting

Check Your Understanding

Given the following code, what will be the contents of the array myHealth after execution?

intPlayerPoints = new int [] {451, 249, -377} Array.Sort(intPlayerPoints);

-377, 249, 451

Page 26: Searching & Sorting

Check Your Understanding

Given the following code, what will be the value of result after execution?

int myLevelPoints = new int [] {203, 478, 1785} Array.Sort(myLevelPoints ); int result = Array.BinarySearch(myLevelPoints , 1785);

2

Page 27: Searching & Sorting

Check Your Understanding

Given the following code, what would be the values in the sub-arrays after the second iteration using a MergeSort?

int myLevels = new int [] {211, 103, 15, 49, 73, 172}

210 103 15 49 73 172 //first 210 103 15 49 73 172 //second

Page 28: Searching & Sorting

Searching & Sorting

For more information on the Array class. http://msdn.microsoft.com/en-us/library/czz5hkty.aspx

For more information on the ArrayList class. http://msdn.microsoft.com/en-us/library/system.collections.arraylist.aspx