Printing the array through each pass (Bubble Sort) - java

im looking for a way to print the array as is after each pass. this is the sort code i have so far. its a basic implementation of the bubble sort algorithm that prints out the original state of the array and the sorted state
public class bubbleSortTest
{
public static void main(String a[])
{
int i;
int array[] = {90, 8, 7, 56, 123, 235, 9, 1, 653};
System.out.println("Values Before the sort:\n");
for(i = 0; i < array.length; i++)
System.out.print( array[i]+" ");
System.out.println();
bubble_srt(array, array.length);
System.out.print("Values after the sort:\n");
for(i = 0; i <array.length; i++)
System.out.print(array[i]+" ");
System.out.println();
System.out.println("PAUSE");
}
public static void bubble_srt( int a[], int n )
{
int i, j,t=0;
for(i = 0; i < n; i++)
{
for(j = 1; j < (n-i); j++)
{
if(a[j-1] > a[j])
{
t = a[j-1];
a[j-1]=a[j];
a[j]=t;
}
}
}
}
}

Add a new for loop inside the outer loop of sorting to print the values after each pass.
for(i = 0; i < n; i++)
{
System.out.print(" After "+(i+1)+"st pass: ");
for(k=0;k<n;k++)
System.out.print(" "+a[k]);
...............
...............

Related

Java Insertion Sort doesnt sort

I have written a java sorting algorithm by insertion, the code compiles but doesnt sort:(. If anyone could point out any flaws, Id be very thankful, Java doesnt...
public class Sort {
public static int[] sort(int[] x) {
int[] y = new int[x.length];
for(int i = 0; i < x.length; ++i) {
int j = 0;
while (y[j] < x[i] && j < i) ++j;
for (int k = i-1; k >= j; --k) y[k+1] = y[k];
y[j]=x[i];
}
return y;
}
public static void main(String[] args) {
int[] size = new int[10];
for(int k=0; k<size.length; ++k ) {
size[k]=(int)(Math.random()*20);
System.out.println(size[k]);
}
System.out.println(sort(size));
}}
[I#39ed3c8d is returned by invoking toString() on an int array, which you then print with System.out.println.
You presumably want System.out.println(Arrays.toString(sort(size))); instead.
You are printing the reference of size i.e., [I#39ed3c8d .
This should help you to understand:
public class Sort {
public static int[] sort(int[] x) {
int[] y = new int[x.length];
for(int i = 0; i < x.length; ++i) {
int j = 0;
while (y[j] < x[i] && j < i) ++j;
for (int k = i-1; k >= j; --k) y[k+1] = y[k];
y[j]=x[i];
}
return y;
}
public static void main(String[] args) {
int[] size = new int[10];
System.out.println("Befor sorting");
for(int k=0; k<size.length; ++k ) {
size[k]=(int)(Math.random()*20);
System.out.print(size[k]);
System.out.print(" ");
}
size = sort(size);
System.out.println("\nAfter sorting");
for(int i = 0; i<size.length; i++){
System.out.print(size[i]);
System.out.print(" ");
}
}}
That random is result of:
System.out.println(sort(size));
Do this instead:
size = sort(size);
for(int k=0; k<size.length; ++k ) {
System.out.print(size[k] + " " );
}

Filling an array with random elements and using SelectionSort but getting weird error: Fix?

class SelectionSort {
public static int[] sort(int[] arr, int n) {
//int min;
int temp;
for(int i=1; i < n; i++) {
int min = i;
for (int j = i+1; j <= n; j++)
if (arr[j] < arr[min])
min = j;
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
return arr;
}
public static int[] fillArray(int[] arr) {
for (int k=0; k < arr.length; k++) {
arr[k] = (int)(Math.random() * 100);
System.out.println(arr[k] + " ");
}
return arr;
}
public static void main(String args[]) {
/* SelectionSort ob = new SelectionSort();
int n = 100;
int[] arr = new int[n];
ob.sort(arr);
ob.fillArray(arr); */
int[] arr1 = fillArray(arr1);
int[] arr2 = sort(arr1);
for (int i:arr2) {
System.out.print(i);
}
}
}
I am getting an odd error saying 'SelectionSort.java:31: error: method sort in class SelectionSort cannot be applied to given types;', any ideas how to fix this? i'm lost.
In the inner for loop you have put "j <= n"
for (int j = i+1; j <= n; j++){
So j become size of the array, but max index should be 1 less than n.

How to do selection sorting in Java?

Here is the my code for a project I am working on for class:
import java.lang.reflect.Array;
public class Project10_MaryEvans {
public static void main(String[] args) {
int[] numbers = {2, 7, 5, 3, 4, 9, 8, 10, 1, 6};
int i = 0;
final int NUMBERS_SIZE = 10;
System.out.print("Unsorted: ");
for (i = 0; i < NUMBERS_SIZE; ++i){
System.out.print(numbers[i] + " ");{
System.out.println();
}
sorting(numbers, NUMBERS_SIZE);
System.out.print("Sorted: ");
for(i = 0; i < NUMBERS_SIZE; ++i){
System.out.print(numbers[i] + " ");
}
System.out.println();
return;
}
}
public static int[] shuffle(int[] numbers){
for(int i = 0; i < numbers.length; ++i) {
numbers[i] = (int)Math.random() * numbers[i];
}
return numbers;
}
public static void sorting(int[] numbers, int numberSize) {
int i = 0;
int j = 0;
int indexSmallest = 0;
int temp = 0;
for (i = 0; i < numberSize; ++i) {
indexSmallest = i;
for(j = i + 1; j < numberSize; ++j) {
if(numbers[j] < numbers[indexSmallest]) {
indexSmallest = j;
}
}
}
}
}
I am not getting the correct output. My output is:
Unsorted: 2
Sorted: 2 7 5 3 4 9 8 10 1 6
I'm juste gonna give you tips here:
Use Arrays.toString(numbers) to print your array easily.
Use numbers.length to get the size of the numbers array.
(the most important one) Your sorting doesn't actually do any sorting, you just set indexes values, but you don't modify the array numbers (numbers[i] = numbers[j] for example).
Your first loop (in main) is useless.
And read the comments.
maybe you have make two for loop nested in your main function, it should be like
System.out.print("Unsorted: ");
for (i = 0; i < NUMBERS_SIZE; ++i){
System.out.print(numbers[i] + " ");
}
System.out.println();
sorting(numbers, NUMBERS_SIZE);
System.out.print("Sorted: ");
for(i = 0; i < NUMBERS_SIZE; ++i){
System.out.print(numbers[i] + " ");
}
System.out.println();
return;
In your sorting function you're forgetting to exchange current element with the minimal found. You're just computing indexes.
Use this method for sorting. Not need int numberSize parameter. It can get through array length. If you need int numberSize parameter replace numbers.length with numberSize.
public static void sorting(int[] numbers) {
int temp;
for (int i = 0; i < numbers.length; i++) {
for (int j = i + 1; j < numbers.length; j++) {
if (numbers[i] > numbers[j]) {
temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
}
Main Method:
public static void main(String[] args) {
int[] numbers = {2, 7, 5, 3, 4, 9, 8, 10, 1, 6};
int i = 0;
final int NUMBERS_SIZE = 10;
System.out.print("Unsorted: ");
for (i = 0; i < NUMBERS_SIZE; ++i){
System.out.print(numbers[i] + " ");
}
System.out.println();
sorting(numbers);
System.out.print("Sorted: ");
for(i = 0; i < NUMBERS_SIZE; ++i){
System.out.print(numbers[i] + " ");
}
}

Sorting an array doesn't take the first element to compare(java programming)?

I'm trying to sort an array that include 5 numbers(size=4) from largest to smallest, but I spend a long time and I don't know why my code don't take the first element to be sorted..
This is My code:
public class sortingS
{
public static void main(String []args)
{
int a[]={5,-2,10,14,-1};
for(int i=0; i<a.length; i++)
{
for(int j=0; j<i; j++)
{
if(a[i]>a[j+1])
{
int temp=a[j+1];
a[j+1]=a[i];
a[i]=temp;
}
}
}
for(int i=0; i<a.length; i++){
System.out.println(a[i]);
}
}
}
and this is the output:
5
14
10
-1
-2
For some reason, you are comparing the element past the current index of j; you're always using j+1. Stop adding 1 to j wherever you use it. You're skipping the first element that way.
if(a[i] > a[j])
{
int temp = a[j];
a[j] = a[i];
a[i] = temp;
}
It's a bubble sort. You can find some explanation here and here.
public static void main(String args[]){
int[] vet = {8, 9, 3, 5, 1};
int aux = 0;
int i = 0;
System.out.println("Original Array: ");
for(i = 0; i<5; i++){
System.out.println(" "+vet[i]);
}
System.out.println(" ");
for(i = 0; i<5; i++){
for(int j = 0; j<4; j++){
if(vet[j] > vet[j + 1]){
aux = vet[j];
vet[j] = vet[j+1];
vet[j+1] = aux;
}
}
}
System.out.println("Ordered Array:");
for(i = 0; i<5; i++){
System.out.println(" "+vet[i]);
}
}

How to make Bubble Sort in Java to output the sorted numbers?

This is my code for the Bubble Sort. I cannot get the actual sorted values to output. The program reads the inputted numbers, but does not print it sorted.
I'm not sure what I have to do to make them sort.
Any advice or suggestions would be helpful.
package sortingalgorithm2;
import java.util.Scanner;
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
Scanner read = new Scanner (System.in);
int[] num = new int[15];
int size = 15;
System.out.println("Enter 15 numbers: ");
for (int i=0; i <= size-1; i++)
{
num[i] = read.nextInt();
}
for (int i=0; i <= size-1; i++)
{
if (num[i] >=1 && num[i] <= 1000)
{
System.out.println("The numbers you entered are: ");
System.out.println(+num[0]);
System.out.println(+num[1]);
System.out.println(+num[2]);
System.out.println(+num[3]);
System.out.println(+num[4]);
System.out.println(+num[5]);
System.out.println(+num[6]);
System.out.println(+num[7]);
System.out.println(+num[8]);
System.out.println(+num[9]);
System.out.println(+num[10]);
System.out.println(+num[11]);
System.out.println(+num[12]);
System.out.println(+num[13]);
System.out.println(+num[14]);
}
else
{
System.out.println("Data input is invalid. Enter a number between "
+
"1 and 1000.");
break;
}
}
BubbleSort (num);
for (int i=0; i < num.length; i++)
{
System.out.println("The sorted numbers are: ");
System.out.print(num[i]+ " ");
}
}
private static void BubbleSort(int[] num)
{
for (int i=0; i <= num.length; i++)
for (int x=1; x <= num.length; x++)
if (num[x] > num[x+1])
{
int temp = num[x];
num[x] = num[x+1];
num[x+1] = temp;
}
}
}
Try this Bubble sort :
private static void BubbleSort(int[] num) {
for (int i = 0; i < num.length; i++) {
for (int x = 1; x < num.length - i; x++) {
if (num[x - 1] > num[x]) {
int temp = num[x - 1];
num[x - 1] = num[x];
num[x] = temp;
}
}
}
}
You are printing the actual numbers in the order the user entered. Try this instead:
int[] sortedNumbers = new int[15];
sortedNumbers = BubbleSort (num);
for (int i=0; i < sortedNumbers.length; i++)
{
System.out.println("The sorted numbers are: ");
System.out.print(sortedNumbers[i]+ " ");
}
public static int[] BubbleSort(int [] num)
{
int temp;
for (int i=1; i<num.length; i++)
{
for(int j=0; j<num.length-i; j++)
{
if (num[j] > num [j+1])
{
temp = num [j];
num [j] = num [j+1];
num [j+1] = temp;
}
}
}
return num;
}
Try this :
for (int i = 0; i < num.length; i++) {
for (int j = i + 1; j < num.length; j++) {
if (num[i] > num[j]) {
num[i] = num[i] + num[j] - (num[j] = num[i]);
}
}
}
you are passing the array variable num (which is not static) to BubbleSort()(which does not returns a value and shadows the global num variable with its own) and trying to use the same num variable to access your sorted array from your main method which is not right.
The genuine fix to this is to declare your variable num as static just before the main method( in the class declaration). So I have made the changes in the program and here is the solution.
import java.util.Scanner;
public class sol {
static int num [] =new int [15]; //declaring num as static in the class definition.
public static void main(String[] args)
{
Scanner read = new Scanner (System.in);
int size = 15;
System.out.println("Enter 15 numbers: ");
for (int i=0; i <= size-1; i++)
{
num[i] = read.nextInt();
}
read.close();
/*for (int i=0; i <= size-1; i++)
{
if (num[i] >=1 && num[i] <= 1000)
{
System.out.println("The numbers you entered are: ");
System.out.println(+num[0]);
System.out.println(+num[1]);
System.out.println(+num[2]);
System.out.println(+num[3]);
System.out.println(+num[4]);
System.out.println(+num[5]);
System.out.println(+num[6]);
System.out.println(+num[7]);
System.out.println(+num[8]);
System.out.println(+num[9]);
System.out.println(+num[10]);
System.out.println(+num[11]);
System.out.println(+num[12]);
System.out.println(+num[13]);
System.out.println(+num[14]);
}
else
{
System.out.println("Data input is invalid. Enter a number between "
+
"1 and 1000.");
break;
}
}*/ //I have disabled this just to check with the sort method.
BubbleSort ();//no need to pass the array as it is static and declared as a //class variable hence can be used to by all the methods of that class
System.out.println("The sorted numbers are: ");
for (int i=0; i < num.length; i++)
{
System.out.print(num[i]+ " ");
}
}
private static void BubbleSort()
{
for (int i=0; i < num.length; i++)// required changes in the looping
for (int x=0; x < num.length-i-1; x++)
if (num[x] > num[x+1])
{
int temp = num[x];
num[x] = num[x+1];
num[x+1] = temp;
}
}
}

Categories

Resources