Data Structures &

Lecture 3 & 4: Searching and Sorting

Halabja University Collage of Science /Computer Science Department Karwan Mahdi [email protected] 2020-2021 Objectives

Introduction to Search Algorithms which is consist of and Binary Search

Understand the significance of sorting

Distinguish between internal and external sorting

Understand the logic behind the simple sorting methods of bubble sort, selection sort, and insertion sort. Introduction to Search Algorithms Search: locate an item in a list of information. Two common algorithms (methods): • Linear search • Binary search

Linear search: also called sequential search. Examines all values in an array until it finds a match or reaches the end. • Number of visits for a linear search of an array of n elements: • The average search visits n/2 elements • The maximum visits is n • A linear search locates a value in an array in O(n) steps

Example for linear search Example: linear of Linear Search

Order in linear search Advantages and Disadvantages • Benefits: • Easy algorithm to understand • Array can be in any order • Disadvantages: • Inefficient (slow): for array of N elements, examines N/2 elements on average for value in array, • N elements for value not in array

“Java code for linear search REQUIRE” Binary Search Binary search is a smart algorithm that is much more efficient than the linear search ,the elements in the array must be sorted in order 1. Divides the array into three sections: • middle element • elements on one side of the middle element • elements on the other side of the middle element 2. If the middle element is the correct value, done. Otherwise, go to step 1. using only the half of the array that may contain the correct value. 3. Continue steps 1. and 2. until either the value is found or there are no more elements to examine.

list

low item middle item high item Is middle item what we are looking for? If not is it more or less than the target item? (Assume lower)

list

low item middle item high item and so forth… Example1: binary search Example1: binary search(conti…) Example1: binary search(conti…) Example2: binary search Java code for Binary Search Java code for Binary Search Advantage and Disadvantages • Benefits: • Much more efficient than linear search. For array of N elements, performs at most log2N comparisons • Disadvantages: • Requires that array elements be sorted

Homework: Re-write the Binary Search code by using Arrays.binarySearch( )and Recursion

Student Work 1: try to read about the following Searching algorims 1) Jump Search 2) 3) Exponential Search 4) Ternary Search Introduction to sorting algorithms • Sorting is a procedure for organizing a collection of data items in ascending or descending order • Sequencing of data item depends on data type • Numerals are arranged according to their magnitude and sign • Characters are arranged according to the character set used. Character set can be ASCII, EBCDIC or Unicode

• Business data are often sorted on some primary and one or more secondary keys. • For example, students result might be arranged by institutions, and, within the institution, by names and grades

Benefits of Sorting • Sorting facilitates the task of searching a large data collection.

• It helps organize business information in a systematic way.

• Sorting is frequently used by DBMS to perform vital database operations. Types of Sorting • Sorting methods are classified into two categories * Internal Sorting * External Sorting • If the data collection to be sorted, is kept in the main memory, sorting is referred to as internal sorting

• In practice, the large amount of data is often stored on disks, or magnetic tapes • The sorting of data stored on secondary device is referred to as external sorting • In external sorting, block of data are loaded into the main memory. The data is sorted in the main memory and written back to the secondary memory device.

• Two algorithms considered here: • Bubble sort • Selection sort Bubble Sort • Bobble sort works by comparing two adjacent data items, and swapping • Array is scanned several times • Sorting is completed in several passes • After first pass, the largest element bubbles up (appears) at the end Bubble Sort Example2: Bubble Sort Concept:  Compare 1st two elements, If out of order, exchange them to put in order  Move down one element, compare 2nd and 3rd elements, exchange if necessary. Continue until end of array.  Pass through array again, exchanging as necessary  Repeat until pass made with no exchanges

Array num_list contains: 17 23 5 11 compare values 23 and 11 – not in correct order,so exchange them

compare values 17 and 23 – in compare values 23 and 5 – not in correct order, so no exchange correct order, so exchange them Example2: Bubble Sort

After first pass, array num_list contains:

17 5 11 23 compare values 17 and 23 – in correct order, so no exchange

compare values 17 and 5 – not in correct order, so exchange them compare values 17 and 11 – not in correct order, so exchange them Example2: Bubble Sort

After second pass, array numlist3 contains:

5 11 17 23 compare values 17 and 23 – in correct order,so no exchange compare values 5 and 11 – in correct order,so no exchange compare values 11 and 17 – in correct order, so no exchange

No exchanges, so array is in order Example3: Bubble Sort Example3: Bubble Sort Example3: Bubble Sort Example3: Bubble Sort Bubble Sort Algorithm

About Bubble Sort Advantages and Disadvantages  Benefit: ◦ Easy to understand and implement  Disadvantage: ◦ Inefficient: slow for large arrays

“Java code for Bubble Sort REQUIRE” Selection Sort • The Selection sort method works in two phases • In first phase we find the largest element in the array • The largest element is swapped with the last element • This process is repeated until the array is sorted

Bubble sort is inefficient for large arrays because items only move by one element at a time. The selection sort performs fewer exchanges because it moves items immediately to their final position in the array.

Concept for sort in ascending order: • Locate smallest element in array. Exchange it with element in position 0 • Locate next smallest element in array. Exchange it with element in position 1. • Continue until all elements are arranged in order Example1:Selection Sort Example1:Selection Sort Example1:Selection Sort

Example2:Selection Sort About Selection Sort Algorithm of Selection Sort Advantages and Disadvantages • Benefit: • More efficient than Bubble Sort, since fewer exchanges • Disadvantage: • May not be as easy as Bubble Sort to understand

“Java code for Selection Sort REQUIRE” Insertion Sort • The insertion sort systematically picks array elements and inserts them into proper positions, Array is divided in two two regions: sorted and un sorted. • In each pass, an element is picked from the unsorted region and moved into a appropriate position in sorted reign Insertion Sort Example2: Insertion Sort Java Code of insertion Sort Student Work 2: try to read about the following sorting algorism 1) Merge Sort 2) Heap Sort 3) QuickSort 4) Radix Sort 5) Counting Sort 6) Bucket Sort 7) ShellSort 8) Comb Sort Question…? 9) Pigeonhole Sort 10) Cycle Sort 11) Thanks  12) Cocktail Sort 13) Strand Sort