What is the purpose of sorts
Sorting algorithms are important not just to java but computer science as a whole. Sorting algorithms sort certain data sets in order. This reduces the difficulty of a computer to solve a problem. We will go over 2 types of sorts in Java.
Bubble Sort
A bubble sort is where the computer compares 2 consecutive indexes in an array.. If the computer sees, for example, that the value at index one is greater than the value at index 2, it will swap index one and index two. This action will be done over and over again until the computer does not have to swap any value in an array.
This is what the code for this would look like in java...
public static void main(String[] args){
int myArray[] = {3,8,1,4,9,10};
//sets the order of an array
for(int i = 0;i < myArray.length; i++){
System.out.print(myArray[i] + " ");
//prints out the array
//output looks like this: 3 8 1 4 9 10
}
System.out.println();
int lastValue;
//an int for storing the last index in an array
for (int j = 0; j < myArray.length; j++) {
for (int i = j + 1; i < myArray.length; i++) {
// comparing indexes next to eachother
if (myArray[i]-myArray[j] < 0) {
//if the value in index+1 is smaller
lastValue = myArray[j];
//saves the smaller value
myArray[j] = myArray[i];
//switches the two values being compared
myArray[i] = lastValue;
//saves the next value to be compared
}
}
}
for(int i = 0;i < myArray.length; i++){
System.out.print(myArray[i] + " ");
//prints out the array
//output looks like this: 1 3 4 8 9 10
}
}
Insertion Sort
An insertion sort marks the first element in an array as sorted. It then compares the next index in an array to the index right before the marker in an array. If the element is already sorted it will move said marker to the next index. If the elements are not sorted, the value on the index before the marker is swapped and all the values before the marker are compared to the value of the swapped index until it finds a value that is smaller than said value.
This is what it looks like in java...
public static void main(String[] args){
int myArray[] = {3,7,9,2,5,4,6};
for(int i = 0;i < myArray.length; i++){
System.out.print(myArray[i] + " ");
//prints out the array
//output looks like this: 3 7 9 2 5 4 6
}
System.out.println();
int length= myArray.length;
for (int j = 1; j < length; j++) {
//for every value of j from the value 1 to the end of the array
int lastValue = myArray[j];
int i = j-1;
//this will get the value of index j - 1
while ( i > -1 && myArray[i] > lastValue ) {
/*this will keep looping as long as
the index being compared is greater than
the first position(0). As well as the saved
value is greater than the last value*/
myArray[i+1] = myArray[i];
//since the last value is greater than the current value
//it will switch with the value at index right after it
i--;
/*It will keep searching for a value that is smaller
than the compared value*/
}
myArray[i+1] = lastValue;
//once sorted it will sort the next index
}
for(int i = 0;i < myArray.length; i++){
System.out.print(myArray[i] + " ");
//prints out the array
//output looks like this: 2 3 4 5 6 7 9
}
}