Upload
cordelia-stephens
View
218
Download
0
Embed Size (px)
DESCRIPTION
Recursion public void recursiveMethod(int number) { if(number == 0) { // base case return; } else { System.out.println(number); // iteration # recursiveMethod(--number); // value change return; } Terminal Window
Citation preview
Recursion
Method calls itself iteratively until a base case is met
and usually containing the following:
• if-else for base case with return value• increment/decrement to advance
closer to base case• recursive call with changed value
upon each successive call
Recursionpublic void recursiveMethod(int number){
if(number == 0) { // base casereturn;
}else {
System.out.println(number); // iteration #recursiveMethod(--number); // value changereturn;
}}
What is the output of recursiveMethod(6)?
Recursionpublic void recursiveMethod(int number){
if(number == 0) { // base casereturn;
}else {System.out.println(number); // iteration #recursiveMethod(--number); // value changereturn;}
} Terminal Window
654321
Recursionpublic void recursiveMethod(int number){
if(number == 0) { // base casereturn;
}else {
System.out.println(number); // iteration #recursiveMethod(--number); // value changeSystem.out.println(number); // after decrementreturn;
}}
Now … output of recursiveMethod(6)?
Recursionpublic void recursiveMethod(int number){
if(number == 0) { // base casereturn;
}else {
System.out.println(number); // iteration #recursiveMethod(--number); // value change System.out.println(number); // after decrementreturn;
}}
Terminal Window654321012345
RecursionWrite a recursive method factorial(int num)
that returns 5! = 5*4*3*2*1 = 120
RecursionWrite a recursive method factorial(int num)
that returns 5! = 5*4*3*2*1 = 120
public int factorial(int num){ if(num == 1) { return 1; } else { return(num*factorial(num-1)); }}
SortingA process that puts elements of a list into
a certain order satisfying the following:• The resulting list is a permutation or
reordering of the original list comprised of the same number of elements
• All resulting elements (in reference with each other) are in increasing/decreasing numerical or alphabetical order
unsortedArray = {8, 4, 6, 1, 9, 3, 2, 5, 7}sortedArray = {1, 2, 3, 4, 5, 6, 7, 8, 9}
Sorting AlgorithmsMany existing sorting algorithms exist:• Selection and Insertion Sorts
– low overhead with use– efficient on small amounts of data– inefficient on large data sets– Insertion sort faster with fewer comparisons
and better for mostly sorted lists– Selection sort uses fewer writes
• Bubble Sort– simple to understand and use– highly inefficient
• Merge Sort– practical and may be more efficient– high overhead with small amounts of data– poor performance on almost sorted lists
Selection SortFIND SMALLEST
Repeatedly finds the smallest element to put into the first position at the beginning
• pass thru list from start to end • store first index i as the min index of smallest value• compare each element value[j] to value[min]• if value[min] > value[j], change to new min = j• at end of pass, swap value[i] and value[min] • 1 pass thru results with smallest value in start index i• repeat iteration for next smallest until complete
int[ ] array = {6, 4, 7, 3}
Write the method selectionSort(int[ ] array) ...
Selection Sortpublic void selectionSort(int[] array){// start with first position at beginning for(int i = 0; i < array.length; i++) {int min = i; // store 1st smallest for(int j = i; j<array.length; j++) {if(array[min] > array[j]) { // compare min = j; // new min found}}int tmp = array[i]; // swap positionarray[i] = array[min]; // with the smallest array[min] = tmp;// 1 pass results with smallest in position i }}
Insertion SortFIND THE PLACE
Repeatedly takes successive elements andfinds the proper place by inserting into the sorted list
• pass thru list from 2nd element at index i to end • store value[i] as the insert value to find proper place for• start index j at same location as index i • compare each element value[j-1] to insert value• while value[j-1] > insert, keep shifting value[j-1] to value[j]• at end of pass, put insert value into proper place at value[j] • results with original insert value at index i in proper place• repeat iteration for next element to place until complete
int[ ] array = {6, 4, 7, 3}Write the method insertionSort(int[ ] array) ...
Insertion Sortpublic void insertionSort(int[] array){
// start with second element at index i for(int i = 1; i < array.length; i++) {
int insert = array[i]; // store value to insertint j = i; // start j same as I
// while position is not found, keep comparing while((j > 0) && (array[j-1] > insert)) {
array[j] = array[j-1]; { // shift value right j--; // look at next left
}array[j] = insert; // put value in place// 1 pass results with original insert value// at index i in its proper place
}}
Bubble SortFIND LARGEST
Repeatedly finds the largest element to put into the last position at the end
• pass thru list from start to end• compare adjacent elements [j] and [j+1]• swap if value[j] > value[j+1]• 1 pass thru results with largest in last position• repeat iteration for next largest until complete
int[ ] array = {6, 4, 7, 3}
Write the method bubbleSort(int[ ] array) ...
Bubble Sortpublic void bubbleSort(int[] array){// start with last position at end for(int i=(array.length-1); i > 0; i--) {for(int j=0; j<i; j++) { if(array[j] > array[j+1]) { // compareint tmp = array[j];array[j] = array[j+1]; // swaparray[j+1] = tmp; }} // 1 pass results with largest in position i }
}
Could this be improved to terminate if elements are all in order (i.e. no swaps occur on a pass)?
Bubble Sortpublic void bubbleSort2(int[ ] array) {
int j = 0;boolean swapped = true;while (swapped) { swapped = false; j++; for (int i = 0; i < array.length - j; i++) {if (array[i] > array[i + 1]) { int tmp = array[i];array[i] = array[i + 1];array[i + 1] = tmp;
swapped = true;} }}
}
Note: swapped signals the end of sorting ...