Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

PRG510S Programming 1

DATA STRUCTURES MANIPULATIONS

Week 8

Searching and Sorting


Week 8 Topics
• Search algorithms
 Linear Search
 Binary Search

 Sorting algorithms, includes:


Simple sorting techniques
 Bubble Sort
 Selection sort
 Insertion sort
Advanced sorting techniques
 Quicksort
 Merge sort
 Radix sort
 Heap sort
 Shell sort

Java Software Structures, 4th Edition, Lewis/Chase 9-2


Searching

 Searching is the process of finding a target element


among a group of items (the search pool), or
determining that it isn't there

Java Software Structures, 4th Edition, Lewis/Chase 9-3


Linear Search

 A linear search simply examines each item in the search


pool, one at a time, until either the target is found or
until the pool is exhausted
 This approach does not assume the items in the search
pool are in any particular order

Java Software Structures, 4th Edition, Lewis/Chase 9-4


Binary Search

 If the search pool must be sorted, then we can be more


efficient than a linear search
 A binary search eliminates large parts of the search pool
with each comparison
 Instead of starting the search at one end, we begin in
the middle
 If the target isn't found, we know that if it is in the pool
at all, it is in one half or the other
 We can then jump to the middle of that half, and
continue similarly

Java Software Structures, 4th Edition, Lewis/Chase 9-5


Binary Search

 A binary search algorithm is often implemented


recursively
 Each recursive call searches a smaller portion of the
search pool
 The base case is when the portion is of size 0

Java Software Structures, 4th Edition, Lewis/Chase 9-6


Sorting
 Sorting is the process of arranging a group of items into a defined order based
on particular criteria
 We must be able to compare one element to another
 Many sorting algorithms have been designed including the following:
 -Simple sorting (Bubble, selection, Insertion sort),
 -Advanced sorting (Merge, Radix, Shell, Quick, Heap sort)

Java Software Structures, 4th Edition, Lewis/Chase 9-7


SELECTION SORT ALGORHTM
 Selection sort orders a list of values by repetitively putting a particular value
into its final position

 Java Program to Perform Selection Sort

 Write a Java program to perform selection sort.

 This example allows one to enter size and items and perform selection sort on
arrays using nested for loops.

Java Software Structures, 4th Edition, Lewis/Chase 9-8


//A Sample Java program for Selection Sort
package Remaining;
import java.util.Scanner;
public class SelectionSort1
{ private static Scanner sc;
public static void main(String[] args)
{
int Size, i, j, temp;
sc = new Scanner(System.in);
System.out.print("Please Enter the Array size = ");
Size = sc.nextInt();
int[] arr = new int[Size];
System.out.format("Enter Array %d elements = ", Size);
for(i = 0; i < Size; i++)
{ arr[i] = sc.nextInt(); }
for(i = 0; i < arr.length - 1; i++)
{ int min = i;
for(j = i + 1; j < arr.length; j++)
{ if(arr[j] < arr[min]) { min = j; } }
temp = arr[min];
arr[min] = arr[i];
arr[i] = temp; }
System.out.print("\nThe Selection Sort Output = ");
for(i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}}} 9-9
THE OUTPUT

9 - 10
Quick Sort
 Quick
sort orders values by partitioning the list
around one element, then sorting each partition
 More specifically:
 choose one element in the list to be the partition (or
pivot) element; we would like the pivot element to be
the median value.
 organize the elements so that all elements less than the
pivot element are to the left and all greater are to the
right, with the pivot element between.
 apply the quick sort algorithm (recursively) to both
partitions
9 - 11
Merge Sort

 Merge sort orders values by recursively


dividing the list in half until each sub-list
has one element, then recombining
 More specifically:
 divide the list into two roughly equal parts
 recursively divide each part in half, continuing
until a part contains only one element
 merge the two parts into one sorted list
 continue to merge parts as the recursion unfolds

9 - 12
Merge Sort

 Dividing lists in half repeatedly:

Java Software Structures, 4th Edition, Lewis/Chase 9 - 13


Merge Sort

 Merging sorted elements

Java Software Structures, 4th Edition, Lewis/Chase 9 - 14


Bubble sort
 bubble sort: orders a list of values by repetitively comparing neighboring
elements and swapping their positions if necessary

 more specifically:
 scan the list, exchanging adjacent elements if they are not in relative order;
this bubbles the highest value to the top
 scan the list again, bubbling up the second highest value
 repeat until all elements have been placed in their proper order

15
"Bubbling" largest element
 Traverse a collection of elements
 Move from the front to the end
 "Bubble" the largest value to the end using pair-wise comparisons and swapping

0 1 2 3 4 5
42 Swap77
42 35 12 101 5
77

16
"Bubbling" largest element
 Traverse a collection of elements
 Move from the front to the end
 "Bubble" the largest value to the end using pair-wise comparisons and swapping

0 1 2 3 4 5

42 35Swap35
77 77 12 101 5

17
"Bubbling" largest element
 Traverse a collection of elements
 Move from the front to the end
 "Bubble" the largest value to the end using pair-wise comparisons and swapping

0 1 2 3 4 5

42 35 12Swap12
77 77 101 5

18
"Bubbling" largest element
 Traverse a collection of elements
 Move from the front to the end
 "Bubble" the largest value to the end using pair-wise comparisons and swapping

0 1 2 3 4 5

42 35 12 77 101 5

No need to swap
19
"Bubbling" largest element
 Traverse a collection of elements
 Move from the front to the end
 "Bubble" the largest value to the end using pair-wise comparisons and swapping

0 1 2 3 4 5

42 35 12 77 5 Swap101
101 5

20
"Bubbling" largest element
 Traverse a collection of elements
 Move from the front to the end
 "Bubble" the largest value to the end using pair-wise comparisons and swapping

0 1 2 3 4 5

42 35 12 77 5 101

Largest value correctly placed


21
Selection sort
 selection sort: orders a list of values by repetitively putting a particular
value into its final position

 more specifically:
 find the smallest value in the list
 switch it with the value in the first position
 find the next smallest value in the list
 switch it with the value in the second position
 repeat until all values are in their proper places

22
Selection sort example

23
Insertion sort
 insertion sort: orders a list of values by repetitively inserting a particular
value into a sorted subset of the list

 more specifically:
 consider the first item to be a sorted sublist of length 1
 insert the second item into the sorted sublist, shifting the first item if needed
 insert the third item into the sorted sublist, shifting the other items as needed
 repeat until all values have been inserted into their proper positions

24
Insertion sort
 Simple sorting algorithm.
 n-1 passes over the array
 At the end of pass i, the elements that occupied A[0]…A[i] originally are still in
those spots and in sorted order.

2 15 8 1 17 10 12 5

0 1 2 3 4 5 6 7
after
2 8 15 1 17 10 12 5
pass 2
0 1 2 3 4 5 6 7
after
1 2 8 15 17 10 12 5
pass 3
0 1 2 3 4 5 6 7
25
Insertion sort example

26

You might also like