The document describes four different sorting algorithms: insertion sort, selection sort, bubble sort, and merge sort. Each algorithm is explained through pseudocode demonstrating how it works to sort an integer array from least to greatest value in ascending order. The algorithms operate by making comparisons between elements in the array and swapping/moving elements into their proper sorted placement.
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
3 views
Prog
The document describes four different sorting algorithms: insertion sort, selection sort, bubble sort, and merge sort. Each algorithm is explained through pseudocode demonstrating how it works to sort an integer array from least to greatest value in ascending order. The algorithms operate by making comparisons between elements in the array and swapping/moving elements into their proper sorted placement.
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4
================================insertion sort (moves the variable into temp,
compares it to the next variable)
public static void main(String[] args) { // main method
int[] ruado = {6,2,5,8,1}; // integer array (array name "ruado" can be changed) for(int i = 1; i <= ruado.length-1; i++) { // loop for the array int temp = ruado[i]; // stores the index in "i" in a temporary storage int j = i; // sets the value of "j" into "i" while(j > 0 && ruado[j-1] > temp) { // loops until the chosen value is less than the temporary value stored in temp ruado[j] = ruado[j-1]; // replaces the value in index "j" with the value in index "j-1" j--; // increment j with - 1 ruado[j] = temp; // replaces the value in index "j" with the temporary value } } System.out.println(Arrays.toString(ruado)); // prints out the whole array
================================selection sort (keeps track of the min value during
the iteration)
public static void main(String[] args) { // main method
int [] enrish = {4,3,7,6,3,8,1,5}; // integer array (array name "enrish" can be changed) for(int i = 0; i< enrish.length -1; i++) { // loop for the array int lowIndex = i; // sets the value of lowIndex to the value of i for(int j = i +1;j< enrish.length ; j++) { // loop number 2 inside of the first loop if(enrish[j] < enrish[lowIndex]) { // compares if the value in index "j" is less than the value in index "lowIndex" lowIndex = j; // sets the value of lowindex into j } } int temp = enrish[i]; // puts the value in index i into temp enrish[i] = enrish[lowIndex]; // puts the value in lowindex into index i enrish[lowIndex] = temp; // puts the value of temp into lowindex } System.out.println(Arrays.toString(enrish)); // prints out the whole array
} ================================bubble sort (checks adjacent variables if theyre in order)
public static void main(String[] args) { // main method
int [] alpax = {6,2,9,0,4,1}; // integer array (array name "alpax" can be changed) for(int i = alpax.length -1; i>= 1;i--) { // loop for the array for(int j = 0;j < i;j++) { // loop 2 for the first loop if(alpax[j]> alpax[j+1]) { // checks to see if the value in index "j" is greater than in index "j-1" int temp = alpax[i]; // places the value of index i into temp alpax[i] = alpax[j]; // replacces the value of index i with the value of index j alpax[j] = temp; // replaces the value of index j with temp } } } System.out.println(Arrays.toString(alpax)); // prints out the whole array }
================================merge sort (splits the array in half then put it
back together in an ordered form)
public static void main(String[] args) { // main method
int [] qwe = {8,2,4,1,6,3,5,0}; // integer array (array name "qwe" can be changed) mergeSort(qwe); // calls out the method merge sort with the array "qwe" System.out.println(Arrays.toString(qwe)); // prints out the whole array }
public static void mergeSort(int [] array) { // mergeSort
method int length = array.length; // creates a length variable of the array if(length <= 1)return; // compares if the length of the array is 1 or less than 1 int middle = length / 2; // sets half of the length of the array into middle int[] leftArray = new int [middle]; //creates a new int array named "leftArray" with the length of the value of middle int[] rightArray = new int [length - middle]; // creates a new int array named "rightArray" with the length of the difference of length and middle int i = 0; // sets the value of i into 0 int j = 0; //sets the value of j into 0 for(; i < length; i++) { // creates a for loop if(i < middle) { // compares the value of i and middle leftArray[i] = array[i]; // places the value of array in index "i" into leftArray index "i" }else { // else rightArray[j] = array[i]; // places the value of array in index "j" into rightArray index "j" j++; // increment j by 1 } } mergeSort(leftArray); // calls out the method mergeSort within itself with the array "leftArray" mergeSort(rightArray); // calls out the method mergeSort within itself with the array "rightArray" merge(leftArray,rightArray,array); //calls out the method merge with the array "leftArray","rightArray" and "array" }
public static void merge(int [] leftArray, int[] rightArray, int [] array) {
// merge method int leftLen = array.length / 2; // creates a length variable for the left array int rightLen = array.length - leftLen; // creates a length variable for the right array int i = 0, l = 0, r = 0; // sets the value of "i", "l" and "r" into 0 while(l < leftLen && r < rightLen) { // creates a while loop if(leftArray[l] < rightArray[r]) { // creates an if statement comparing the left array with the right array array[i] = leftArray[l]; // sets the value of array in index i to the value of leftArray in index l i++; // increment i by 1 l++; // increment l by 1 }else { // else array[i] = rightArray[r]; // sets the value of array in index i to the value of rightArray in index r r++; // increment r by 1 i++; // increment i by 1 } } while(l < leftLen) { // while loop for left array array[i] = leftArray[l]; // sets the value of array in index i to the value of leftArray in index l i++; // increment i by 1 l++; // increment l by 1 } while(r < rightLen) { // while loop for right array array[i] = rightArray[r]; // sets the value of array in index i to the value of rightArray in index r i++; // increment i by 1 r++; // increment r by 1 } } ================================quick sort (finding the proper placement of a chosen value)
public static void main(String[] args) { // main
method int [] asd = {9,3,1,2,5,6,8,4}; // integer array (array name "asd" can be changed) quickSort(asd,0,asd.length-1); // calls out the method "quickSort" System.out.println(Arrays.toString(asd)); // prints out the array }
public static void quickSort(int [] array, int start, int end) { //
creates a method called quickSort if(end <= start) return; // compares if the end is less than or equal to start int pivot = partition(array,start,end); // creates a variable pivot using the "partition" method quickSort(array,start, pivot-1); // calls quicksort within itself for the first part of the sorting quickSort(array,pivot + 1, end); // calls quicksort within itself for the second part of the sorting
public static int partition(int [] array, int start, int end) {
// creates a method called partition to help the quickSort method int pivot = array[end]; // creates another pivot variable int i = start - 1; // creates an i variable for(int j = start; j <= end - 1; j++) { // loop for the sorting if(array[j] < pivot) { // compares the value of array index "j" to pivot i++; // increment i by 1 int temp = array[i]; // stores the value of array index "i" into temp variable array[i] = array[j]; // replaces the value of array index "i" with array index "j" array[j] = temp; // replaces the value of array index "j" with temp } } i++; // increment i by 1 int temp = array[i]; // stores the value of array index "i" into temp variable array[i] = array[end]; // replaces the value of array index "i" with array index "end" array[end] = temp; // replaces the value of array index "end" with temp return i; // return the i }