public class BasicSortings {
public static void main(String[] args) {
int temp;
int iterationCount = 0;
int array[] = { 2, 33, 29, 30, 21, 98};
//Bubble sort or Simple sort
for (int i = 0; i < array.length - 1; i++) { for (int j = i + 1; j < array.length; j++) { if (array[i] > array[j]) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
iterationCount++;
}
}
System.out.println("Bubble Sort Big 0(n) --> " + iterationCount);
for (int s = 0; s < array.length; s++) { System.out.print(array[s] + "\t"); } System.out.println("\n"); System.out.println("\n"); //Selection Sort
iterationCount =0;
int sortPointer=0;
for (int i = sortPointer; i < array.length; i++) { for(int j=i+1;j< array.length;j++) { if(array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
iterationCount++;
}
sortPointer++;
}
System.out.println("Selection Sort Big 0(n) --> " + iterationCount);
for (int s = 0; s < array.length; s++) { { System.out.print(array[s] + "\t"); } } System.out.println("\n"); //System.out.println("\n"); //Insertion Sort
iterationCount=0;
for (int i = 1; i < array.length; i++) //Outer Loop travers forwards { for(int j=i;j>0;j--) // Inner loop travers back wards
{
if(array[j-1]>array[j])
{
temp = array[j-1];
array[j-1] = array[j];
array[j] = temp;
}
iterationCount++;
}
}
System.out.println("Insertion Sort Big 0(n) --> " + iterationCount);
for (int s = 0; s < array.length; s++) {
{
System.out.print(array[s] + "\t");
}
}
}
}
public static void main(String[] args) {
int temp;
int iterationCount = 0;
int array[] = { 2, 33, 29, 30, 21, 98};
//Bubble sort or Simple sort
for (int i = 0; i < array.length - 1; i++) { for (int j = i + 1; j < array.length; j++) { if (array[i] > array[j]) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
iterationCount++;
}
}
System.out.println("Bubble Sort Big 0(n) --> " + iterationCount);
for (int s = 0; s < array.length; s++) { System.out.print(array[s] + "\t"); } System.out.println("\n"); System.out.println("\n"); //Selection Sort
iterationCount =0;
int sortPointer=0;
for (int i = sortPointer; i < array.length; i++) { for(int j=i+1;j< array.length;j++) { if(array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
iterationCount++;
}
sortPointer++;
}
System.out.println("Selection Sort Big 0(n) --> " + iterationCount);
for (int s = 0; s < array.length; s++) { { System.out.print(array[s] + "\t"); } } System.out.println("\n"); //System.out.println("\n"); //Insertion Sort
iterationCount=0;
for (int i = 1; i < array.length; i++) //Outer Loop travers forwards { for(int j=i;j>0;j--) // Inner loop travers back wards
{
if(array[j-1]>array[j])
{
temp = array[j-1];
array[j-1] = array[j];
array[j] = temp;
}
iterationCount++;
}
}
System.out.println("Insertion Sort Big 0(n) --> " + iterationCount);
for (int s = 0; s < array.length; s++) {
{
System.out.print(array[s] + "\t");
}
}
}
}
Comments
Post a Comment