Reputation: 303
For some reason I can't get the count for comparisons and swap in the InsertionSort part, it just outputs zero. And when I isolate the code for it, it outputs a number of swaps and comparisons (though I don't know if it's wrong or not, probably wrong considering the number is the same for both) and the array is not sorted at all. I'm really confused as to why this isn't working, any help is greatly appreciated!
Update: The instance of bubble was being passed onto both selection and insertion, now that that's fixed turns out T also have a problem with the selection part. Any suggestions on how to fix them?
Update 2: Fixed the selection part! Still confused about insertion.
import java.util.Scanner;
public class Sorting {
public static void main(String[] args) {
int n, c;
Scanner scan = new Scanner(System.in);
System.out.print("Number of elements: ");
n = scan.nextInt();
int[] bubbleSortArray = new int[n];
int[] selectionSortArray = new int[n];
int[] insertionSortArray = new int[n];
System.out.print("Enter " + n + " elements: ");
for (c = 0; c < n; c++) {
int i = scan.nextInt();
bubbleSortArray[c] = i;
selectionSortArray[c] = i;
insertionSortArray[c] = i;
}
BubbleSort(bubbleSortArray);
SelectionSort(selectionSortArray);
InsertionSort(insertionSortArray);
}
static void BubbleSort(int[] array) {
int n = array.length;
int cm = 0;
int sw = 0;
for (int c = 0; c < (n - 1); c++) {
for (int d = 0; d < n - c - 1; d++) {
cm++;
if (array[d] > array[d + 1]) {
int swap = array[d];
array[d] = array[d + 1];
array[d + 1] = swap;
sw++;
}
}
}
System.out.print("Bubble sort: ");
for (int c = 0; c < n; c++) {
System.out.print(array[c] + " ");
}
System.out.println("- " + cm + " comparisons, " + sw + " swaps");
}
static void SelectionSort(int[] array) {
int n = array.length;
int cm = 0;
int sw = 0;
for (int c = 0; c < n - 1; c++) {
int index = c;
for (int d = c + 1; d < n; d++){
cm++;
if (array[d] < array[index])
index = d;
}
int temp = array[index];
sw++;
array[index] = array[c];
array[c] = temp;
}
System.out.print("Selection sort: ");
for (int c = 0; c < n; c++) {
System.out.print(array[c] + " ");
}
System.out.println("- " + cm + " comparisons, " + sw + " swaps");
}
static void InsertionSort(int[] array) {
int n = array.length;
int cm = 0;
int sw = 0;
for (int c = 1; c < n; c++){
int temp = array[c];
for (int d = c - 1; d > 0 && temp < array[d]; d--) {
array[d+1] = array[d];
array[d+1] = temp;
cm++;
sw++;
}
}
System.out.print("Insertion sort: ");
for (int c = 0; c < n; c++) {
System.out.print(array[c] + " ");
}
System.out.println("- " + cm + " comparisons, " + sw + " swaps");
}
}
Upvotes: 0
Views: 2361
Reputation: 650
After you are done with BubbleSort array is sorted, and you are passing that sorted instance to SelectionSort and InsertionSort.
If you want to get the results for each kind of sorts you can do :
int n, c;
Scanner scan = new Scanner(System.in);
System.out.print("Number of elements: ");
n = scan.nextInt();
int[] bubbleSortArray = new int[n];
int[] selectionSortArray = new int[n];
int[] insertionSortArray = new int[n];
System.out.print("Enter " + n + " elements: ");
for (c = 0; c < n; c++) {
int i = scan.nextInt();
bubbleSortArray[c] = i;
selectionSortArray[c] = i;
insertionSortArray[c] = i;
}
BubbleSort(bubbleSortArray);
SelectionSort(selectionSortArray);
InsertionSort(insertionSortArray);
Upvotes: 1