Algorithms and Sorting: A Beginner's Guide to Java, Study notes of Computer Science

An introduction to algorithms, focusing on sorting techniques such as bubble sort and selection sort. It covers preconditions, loop invariants, postconditions, and running time analysis. Students will learn about the best and worst-case scenarios for these algorithms and gain insights into their implementation.

Typology: Study notes

Pre 2010

Uploaded on 03/28/2010

koofers-user-qm8-1
koofers-user-qm8-1 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Introduction to Programming
with Java for Beginners
with
Java
,
for
Beginners
Algorithms
Sorting
Search
Algorithm
Algorithm
A “recipe”; a finite sequence of steps to complete
atask
a
task
.
Examples: searching, sorting, shortest
path
1
Running time
Best case, worst case, average case running
times for large n.
Buzz Words
Preconditions
Things that should be true before executing an algorithm
Postconditions
Things that should be true after executing an algorithm
Loop Invariant
A true statement that we can make about each step of the loop. i.e. At step i, (some
statement) is true or a condition that doesn’t change
Running Time of an Algorithm - Big O
Running time in terms of a large input, n. For example:
O(n) is n^2 ->Big O of n is n squared i.e. for a large input n, the running time is n squared
Divide and Conquer Algorithm
2
Divide
and
Conquer
Algorithm
Solve a problem by dividing it in to pieces.
Key
When searching, it’s the thing we’re looking for
Sorting an Array of Integers
10 30 5 2 6 12 18
Preconditions: an unsorted (or sorted) array
Algorithms: selection, bubble, …
Loop Invariant: Depends on the algorithm
Postcondition: the array is sorted
3
2 5 6 10121830
pf3
pf4

Partial preview of the text

Download Algorithms and Sorting: A Beginner's Guide to Java and more Study notes Computer Science in PDF only on Docsity!

Introduction to Programming

with Java for Beginnerswith

Java, for Beginners

AlgorithmsSortingSearch

Algorithm

„^ Algorithm^ A “recipe”; a finite sequence of steps to complete

a taska task.

„^ Examples: searching, sorting, shortestpath

1

„^ Running time^ Best case, worst case, average case running

times for large n.

Buzz Words

„^ Preconditions^ „

Things that should be true before executing an algorithm „^ Postconditions^ „

Things that should be true after executing an algorithm „^ Loop Invariant^ „

A true statement that we can make about each step of the loop. i.e. At step i, (somestatement) is true or

a condition that doesn’t change

„^ Running Time of an Algorithm - Big O^ „

Running time in terms of a large input, n. For example: „ O(n) is n^2 ->Big O of n is n squared i.e. for a large input n, the running time is n squared „^ Divide and Conquer Algorithm

2

„^ Divide

and Conquer Algorithm „ Solve a problem by dividing it in to pieces. „^ Key^ „

When searching, it’s the thing we’re looking for

Sorting an Array of Integers

„^ Preconditions: an unsorted (or sorted) array „^ Algorithms: selection, bubble, … „^ Loop Invariant: Depends on the algorithm „^ Postcondition: the array is sorted

3

Bubble Sort

„^ Compare each element (except the last one) with its neighbor tothe right^ „

If they are out of order, swap them „ This puts the largest element at the very end „ The last element is now in the correct and final place„ The last element is now in the correct and final place „^ Compare each element (except the last

two ) with its neighbor to

the right^ „^ If they are out of order, swap them^ „^ This puts the second largest element next to last^ „^ The last two elements are now in their correct and final places „ Compare each element (except the last

three

) with its neighbor to

4

„^ Compare each element (except the last

three

) with its neighbor to

the right^ „^ Continue as above until you have no unsorted elements on the left „ This causes larger values to "bubble" to the end of the list whilesmaller values "sink" towards the beginning of the list.

Bubble Sort

„^

Preconditions: array may or may not be sorted „^

Algorithm:start = 0, end = (array length - 1)Loop:

Compare successive pairs of items and put the larger “on the right”This will “bubble” the largest one all the way to the right.start = start + 1If (start == end) we’re done. The array is sorted. „^

Loop Invariant: At step i, the i largest items have been placed.

5

„^

Postcondition: Array is sorted „^

What is the best/worst running time given n items? Best and worst time:(n-1) + (n-2) + … 1 = (n+1)(n)/2 which is approximately n^2If the array is already sorted AND the bubblesort is optimized toquit if no switches are done, then the best time is n.

Bubble Sort Code

public static void bubbleSort(int[] a){for (int i=0; i < a.length - 1; i++) {

// if a pass is made with no swaps, we're done.boolean swappedElements = false;for (int j=0; j< a.length - 1 - i; j++){// compare neighbors. if the earlier one is bigger, swap.if (a[j] > a[j + 1]) {

int tmp = a[j];a[j] = a[j+1];a[j+1] = tmp;swappedElements = true;}

6

} }if (! swappedElements)break; }}

Selection Sort

„^

Preconditions: an unsorted (or sorted) array „^

Algorithm

Algorithm

start = 0, end = (array length - 1)Loop:If (start == end) we’re doneFind smallest item in series array[start] to array[end]Switch array[start] with the smallest item.start = start + 1 „^

Loop Invariant: After step i, the first i items in the array contain thesmallest i items in sorted order.

7

„^

Postcondition: Array is sorted „^

Running time (average – best & worst) „ Selecting the lowest element requires scanning all

n^ elements (this takes

n

−^ 1 comparisons) and then swapping it into the first position. „^

Finding the next lowest element requires scanning the remaining

n^ −^1

elements and so on n + (n-1) + (n-2) + … 1 = (n+1)(n)/2 which is approximately n^

Binary Search

„^

Preconditions: a

sorted

array and a “key”

„^

Algorithm:Compare the key with the array’s

middle

item

(a^ divide & conquer

algorithm)

Compare the key with the array s

middle

item

Loop:If they’re the same, we’re done (return the index)If the key is smaller, consider the left subarrayIf the key is larger, consider the right subarrayIf the subarray size is 0, we’re done (return failure)Otherwise, compare key with the subarray’s

middle

item

12

Otherwise, compare key with the subarray s

middle

item

„^

Loop Invariant: After step i, the size of the dataset yet to be searched is(array length / 2^i) „^

Postcondition: the index of an occurrence of the key is returned, or

an error message is transmitted if the key is not found

„^

Best case running time? Worst case?

1 step, log n steps