Counting number of data swaps in Java bubble sort - java

I'm trying to count and output the number of swaps of data elements in a bubble sort method using Java. I'm a beginner, so am unsure how to overcome my issue: method is void and can't return anything, so what can I do to output the swaps?
in one doc, the sort method:
public class SearchSortAlgorithms<T> implements SearchSortADT<T>
{
//Bubble sort algorithm.
//Postcondition: list objects are in ascending order.
public void bubbleSort(T list[], int length)
{
//Initialize swap counter
int bubbleSwaps = 0;
for (int iteration = 1; iteration < length; iteration++)
{
for (int index = 0; index < length - iteration;
index++)
{
Comparable<T> compElem =
(Comparable<T>) list[index];
if (compElem.compareTo(list[index + 1]) > 0)
{
T temp = list[index];
list[index] = list[index + 1];
list[index + 1] = temp;
bubbleSwaps++;
}
}
}
}
}
In another, I use the method, and am looking to find a way to output bubbleSwaps:
public class numberSwapsReview{
public static void main(String [] args)
{
// define an Integer array of 1000 elements
Integer[] bubbleArray = new Integer[1000];
// load the array with random numbers using
// a for loop and Math.random() method - (int)(Math.random()*1000)
for (int i = 0; i < bubbleArray.length; i++) {
bubbleArray[i] = (int)(Math.random() * 1000);
}
SearchSortAlgorithms<Integer> sortObject = new SearchSortAlgorithms<Integer>();
sortObject.bubbleSort(bubbleArray, 1000);
}
}

Since you are making an instance of the class, you could make bubbleSwaps class level then get the value after the sort
public class SearchSortAlgorithms<T> implements SearchSortADT<T>
{
//Initialize swap counter
int bubbleSwaps = 0;
//Bubble sort algorithm.
//Postcondition: list objects are in ascending order.
public void bubbleSort(T list[], int length)
{
for (int iteration = 1; iteration < length; iteration++)
{
for (int index = 0; index < length - iteration;
index++)
{
Comparable<T> compElem =
(Comparable<T>) list[index];
if (compElem.compareTo(list[index + 1]) > 0)
{
T temp = list[index];
list[index] = list[index + 1];
list[index + 1] = temp;
this.bubbleSwaps++;
}
}
}
}
public int getBubbleSwaps(){
return this.bubbleSwaps;
}
}
Here is the other class:
public class numberSwapsReview{
public static void main(String [] args)
{
// define an Integer array of 1000 elements
Integer[] bubbleArray = new Integer[1000];
// load the array with random numbers using
// a for loop and Math.random() method - (int)(Math.random()*1000)
for (int i = 0; i < bubbleArray.length; i++) {
bubbleArray[i] = (int)(Math.random() * 1000);
}
SearchSortAlgorithms<Integer> sortObject = new SearchSortAlgorithms<Integer>();
sortObject.bubbleSort(bubbleArray, 1000);
sortObject.getBubbleSwaps();
}
}

You might use a Comparator instead of Comparable elements. With that (preferable as a wrapper around the normal comparator), you can compute in a field variable how many time was the result >0

One way would be to change the return type of your method as int and return the number of swaps you did. Then just call:
int nbswaps = sortObject.bubbleSort(bubbleArray, 1000);
If you can't modify the return type, you can
print the number of swaps in the method at the end
create an attribute named nbSwaps in your class (don't forget to reset it at each call of bubbleSort method) and then implement a getter to be able to get the value from your main
Also you may change the declaration of your class as class SearchSortAlgorithms<T extends Comparable<T>>.
Now you will be sure that the elements have to be comparable and you could get rid of Comparable<T> compElem = (Comparable<T>) list[index];.

Related

REPLACEMENT SELECTION SORT:

I am working on Replacement Selection sort project but i keep getting the error Exception in thread main Java.lang.ArrayIndexOutOfBoundsException:10 at ReplacementSelection.swap(ReplacementSelection.java:42) at ReplacementSelection.siftDown(ReplacementSelection.java:69) at Replacement..
class ReplacementSelection {
static int[] array = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
public static void sort() {
System.out.println("before:" + Arrays.toString(array));
for (int i = array.length/2; i >= 0; i--) {
siftDown(i);
}
int count = array.length-1;
while (count > 0)
{
swap(array[0], array[count]);
--count;
siftDown(0);
}
System.out.println("after:" + Arrays.toString(array));
}
public static void swap(int i, int j)
{
int tmp;
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
public static void siftDown(int index)
{
int count = array.length;
// Left child is at index*2+1. Right child is at index*2+2;
while (true)
{
// first find the largest child
int largestChild = index*2+1;
// if left child is larger than count, then done
if (largestChild >= count)
{
break;
}
// compare with right child
if (largestChild+1 < count && array[largestChild] < array[largestChild+1])
{
++largestChild;
}
// If item is smaller than the largest child, then swap and continue.
if (array[index] < array[largestChild])
{
swap(array[index], array[largestChild]);
index = largestChild;
}
else
{
break;
}
}
}
public static void main(String[] args){
ReplacementSelection a = new ReplacementSelection();
a.sort();
}
}
You have written a swap method which takes indices as arguments. However, you pass it the values in the array at those indices instead of the indices themselves:
swap(array[0], array[count]);
and
swap(array[index], array[largestChild]);
To fix the exception error just pass the indices to the method:
swap(0, count);
and
swap(index, largestChild);
As #Pajacar123 mentioned, you should learn to use debugger.
In line
swap(array[index], array[largestChild]);
You are passing value from array which is at last index of table(index 9 value 10). Then when in method sawp in line array[i] = array[j];
j value is 10 while max index of table is 9. That causes exception. You are trying to refer to not existing elemnt.

Print array in a loop with dynamic variable Java

I am trying to define an array that increased by one each time then print the result of an equation that the array is assigned to;
my code so far is
public class PrimeNumberCounter {
public static void main(String[] args) {
// Define integers
int a = 1;
final int b = 2;
final int c = 3;
// Define Array
int[] nth;
nth = new int[100];
//Set loop
for (a = 1; a < 100; a = a + 1) {
int d = a * b + c;
System.out.println(d);
}
}
}
The math function and loop work but I can't figure out how to publish the nth term.
So far I have read array's are the most suitable solution but after much messing around removed the print array part, and decided to ask for help.
Thanks All
EDIT: addition of original code (it's kinda silly hence why I didn't publish it 1st time
public class PrimeNumberCounter {
public static void main(String[] args) {
// Define integers
int a = 1;
final int b = 2;
final int c = 3;
// Define Array.
int[] nth;
nth = new int[100];
//Set loop
for (a = 1; a < 100; a = a + 1) {
nth[] = a * b + c;
System.out.println(nth);
}
}
}
Use some collection like a list instead of using array.
You can use an iterator to iterate over that list and update the iterator.
If I understood you correctly, you are not trying to increase the size of the array, what you are trying to do is increase a counter that iterates through the array and assigns the result of the d = a * b + c; equation to the ith element of the array. Then after all the elements in the array have been populated, you want to iterate through the array an print all the values. If that is the case you can modify your code as follows:
public class PrimeNumberCounter {
public static void main(String[] args) {
// Define integers
int a = 1;
final int b = 2;
final int c = 3;
//int d = 0; // not used
// Define Array assuming that its size is fixed to 100
int[] nth = new int[100];
// Set the assignment loop
for (a = 1; a <= nth.length; ++a) {
// array indexing starts at 0
nth[a - 1] = a * b + c;
}
// print loop
for (int i = 0; i < nth.length; ++i) {
System.out.printf("%d: %d\n", i + 1, nth[i]);
}
}
}
If you don't know the size of the array a priori you can use an ArrayList instead which can grow dynamically as mentioned in the comments and previous answer.
import java.util.List;
import java.util.ArrayList;
public class PrimeNumberCounter {
public static void main(String[] args) {
// Define integers
int a = 1;
final int b = 2;
final int c = 3;
// Some size N let's assume it's a 100 again
List<Integer> nth = new ArrayList<Integer>();
// Set the assignment loop
for (a = 1; a <= 100; ++a) {
// array indexing starts at 0
nth.add(a - 1, a * b + c);
}
// print loop
for (int i = 0; i < nth.size(); ++i) {
System.out.printf("%d: %d\n", i + 1, nth.get[i]);
}
}
}

Given an array of integers, find out the third largest value in the array

public int thirdLargest(int[] arr){
int f_l = arr[0];
int s_l = arr[0];
int t_l = arr[0];
for(int i=1;i<arr.length;i++)
{
if (f_l < arr[i]){
t_l = s_l;
s_l = f_l;
f_l = arr[i];
}
else if (s_l < arr[i]){
t_l = s_l;
s_l = arr[i];
}
else if (t_l < arr[i]){
t_l = arr[i];
}
}
return t_l;
}
my code didn't passes some cases,any suggestion?
parameter {24,27,30,31,34,37,40,42}' , passes
parameter {2,-1,-2,-3,-4,-5}' , fails
This is simply cause by the fact that you initialize all values to arr[0]. If all elements are smaller than arr[0] this code won't update the values, even though the second-largest element for example wouldn't be arr[0]. Instead initialize the variables for the third/second/largest value with Integer.MIN_VALUE and start the search with the first element (index = 0) instead of the second.
There is actually a well-known algorithm for this, which is more generic than yours. It is called quick-select and looks like a quick sort with an optimization making it faster (linear time in average) : since we don't need to sort the array, we just recurse on the part of the array containing the index we are looking for (in your case, third item so index 2).
Here is an implementation in Java :
private static final Random rd = new Random();
public static int kthElement(int[] arr, int k) {
return kthElement(arr,k,0,arr.length);
}
private static T kthElement(int[] arr, int k, int min, int max) {
if (min < max - 1) {
int p = pivot(arr,min,max);
return p == k - 1 ? arr[p] :
p < k - 1 ? kthElement(arr,k,p + 1,max) : kthElement(arr,k,min,p);
}
return arr[min];
}
private static int pivot(int[] arr, int min, int max) {
int pivot = min + rd.nextInt(max - min);
swap(arr,pivot,max - 1);
pivot = min;
for (int i=min ; i<max ; i++)
if (arr[i] < arr[max - 1]) swap(arr,i,pivot++);
swap(arr,max - 1,pivot);
return pivot;
}
private static void swap(int[] arr, int i, int j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
Well, as an alternative to your working code, here is a solution that will allow you to find the Nth largest integer in your array using Collections to do the heavy lifting:
import java.util.Arrays;
import java.util.Collections;
public class ArrayNthLargest {
public static int getNthLargest(int[] arrayInput, int n) {
Integer[] sortedArray = new Integer[arrayInput.length];
for (int i = 0; i < arrayInput.length; i++) {
sortedArray[i] = new Integer(arrayInput[i]);
}
Arrays.sort(sortedArray, Collections.reverseOrder());
return (sortedArray[n - 1]);
}
public static void main(String[] args){
int nth = new Integer(0);
int n = new Integer(3);
int[] testArray = {1,2,3,4,5,6,23,44,55,8,1};
nth = getNthLargest(testArray, n);
System.out.printf("The %d sorted array value is %d", n, nth);
}
}
This was actually an interesting question to me to do in O(n) complexity. I hope this solution is order n. I used an ArrayList as a stack (since Stack object won't allow addition of items in specific incidences (I've generalized it).
public int thirdLargest(int[] arr){
public int N_TH = 3; // Assuming this is nth largest you want
public ArrayList<Integer> largest = new ArrayList<Integer>(N_TH);
for(int i = 0;i<N_TH;i++)
largest.add(0); // initialize the ArrayList
for(int i = 0;i<arr.length;i++) {
for(int j=0;j<largest.size();j++){
if(arr[i] >= largest.get(j)) {
// Add the item at the correct index
// Pop the last element
largest.remove(largest.size()-1);
largest.add(j,arr[i]);
break;
}
}
}
return largest.get(N_TH);
}
Let me know if you find any problems with it, I might have mistyped part of trying to put it in OP's method.
EDIT won't work with negative numbers at the moment. You can find the smallest value in arr and initialize largest with that value. Then it'll also with negative numbers

inserting and reversing elements in an array in java

I have a problem of using an insert method and reverse method that i am trying to make within a constructor as i am new and this is an exercise example. For the inserting constructor i have:
It is supposed to insert an element at index in the array
public boolean insert(int index, int element)
{
int i;
newArray = new int[Array.length + 1];
for(i = index; i > newArray.length - 1; i++)
{
newArray[i] = newArray[i + 1];
for(i = 0; i < newArray.length + 1; i++)
{
Array[i] = newArray[i];
}
}
Array[index] = element;
numElement = numElement + 1;
return true;
and my reverse constructor is:
public boolean reverse(int start, int end)
{
int temp;
for(int i = start; i <(start + end) / 2 ; i++)
{
temp = Array[i];
Array[i] = Array[end - i];
Array[end - i] = temp;
}
return true;
}
when i use the insert constructor it would replace the number and not make a new array to accomodate the extra element and before the output closes it would give me an exception error. For the reverse, it would not give me the correct output. Eg: i have an array [1,2,3,4,5,6,7,8,9,10] and i want to reverse numbers 3-7 it would output [1,2,6,5,4,3,7,8,9,10]. I don't know what could be wrong with the code.
EDIT: Also i am trying not to use any Arrays.util methods/ArrayList and such
There is a Java reverse method for arrays:
ArrayUtils.reverse(int[] array) //Reverses the order of the given array.
And also for add:
ArrayUtils.add(int[] array, int element) //Copies the given array and adds the given element at the end of the new array.
If not using ArrayUtils (why not?), then perhaps use System.arraycopy for the insert method.
Also, these are not constructors, they are methods. They probably should also be void methods or return the new array and not a boolean (unless there is a reason to return a boolean here?).
Example insert method using System.arraycopy:
public static int[] insert(int[] list, int position, int value)
{
int[] result = new int[list.length + 1];
System.arraycopy(list,0,result,0,position);
result[position] = value;
System.arraycopy(list,position,result,position + 1,list.length - position);
return result;
}

bubbleSort() for an int Array Linked List in Java

I'm having trouble with a bubbleSort method for my very unique homework assignment.
We are supposed to use a sorting method of our choice to sort, get this, a linked list of int arrays. Not an ArrayList not just a LinkedList. It works like a linked list but the each Node contains an array of a capacity of 10 ints.
I am stuck on the sorting method. I chose bubbleSort just because it was used in the last assignment and I felt most familiar with it. Any tips for a better sorting method to try would be considered helpful as well.
Here is my code:
public void bubbleSort() {
current = head; // Start at the head ArrayNode
for (int i = 0; i < size; i++) { // iterate through each ArrayNode
currentArray = current.getArray(); // get the array in this ArrayNode
int in, out;
for (out = size-1; out > 1; out--) { // outer loop (backwards)
for (in = 0; in < out; in++) { // inner loop (forwards)
if (currentArray[in] > currentArray[in+1]) // out of order?
swap(in, in+1); // swap them!
}
}
current.setArray(currentArray);
current = current.getNext();
}
}// End bubbleSort() method
// A helper method for the bubble sort
private void swap(int one, int two) {
int temp = currentArray[one];
currentArray[one] = currentArray[two];
currentArray[two] = temp;
} // End swap() method
This is a picture example of what I am supposed to be doing.
I have found a solution with selectionsort. There are a few test values, just run it to see it.
I can provide further information if needed.
import java.util.ArrayList;
import java.util.Random;
public class ArrayedListSort {
int listsize = 5; // how many nodes
int maxValue = 99; // the highest value (0 to this)
int nodeSize = 3; // size for every node
public static void main(String[] args) {
// run non static
new ArrayedListSort().runTest();
}
/**
* Log function.
*/
public void log(Object s) {
System.out.println(s);
}
public void logNoBR(Object s) {
System.out.print(s);
}
/**
* Output of list we have.
*/
public void logMyList(ArrayList<ListNode> listNode, String name) {
log("=== LOG OUTPUT " + name + " ===");
for ( int i=0; i < listNode.size(); i++) {
logNoBR(" node <" + i + ">");
logNoBR(" (");
for (int j=0; j < listNode.get(i).getSize(); j++) {
if ( j != (listNode.get(i).getSize()-1)) // if not last add ","
logNoBR( listNode.get(i).getValueAt(j) + "," );
else
logNoBR( listNode.get(i).getValueAt(j) );
}
log(")");
}
log("=====================================\n");
}
public void runTest() {
// create example List
ArrayList<ListNode> myList = new ArrayList<ListNode>();
// fill the nodes with random values
for ( int i = 0; i < listsize; i++) {
myList.add(new ListNode(nodeSize));
for (int j=0; j < nodeSize; j++) {
int randomValue = new Random().nextInt(maxValue);
myList.get(i).addValue(randomValue);
}
}
logMyList(myList, "myList unsorted"); // to see what we have
// now lets sort it
myList = sortListNode(myList);
logMyList(myList, "myList sorted"); // what we have after sorting
}
/**
* Selectionsort
*/
public ArrayList<ListNode> sortListNode(ArrayList<ListNode> myList) {
ArrayList<ListNode> retList = new ArrayList<ListNode>();
for ( int i = 0; i < listsize; i++) {
retList.add(new ListNode(nodeSize));
}
int lastSmallest = myList.get(0).getValueAt(0);
while ( !myList.isEmpty() ) {
int lastJ=0, lastI=0;
for ( int i = 0; i < myList.size(); i++) {
for (int j=0; j < myList.get(i).getSize(); j++) {
if ( myList.get(i).getValueAt(j) <= lastSmallest ) {
lastSmallest = myList.get(i).getValueAt(j);
lastJ = j;
lastI = i;
//log("Found smallest element at <"+i+","+j+"> (" + lastSmallest + ")");
}
}
}
myList.get(lastI).removeValue(lastJ);
if ( myList.get(lastI).getSize() == 0 )
myList.remove(lastI);
// add value to new list
for ( int i = 0; i < listsize; i++) {
if ( retList.get(i).getSize() < retList.get(i).getMaxSize() ) {
retList.get(i).addValue(lastSmallest);
break;
}
}
lastSmallest = Integer.MAX_VALUE;
}
return retList;
}
public class ListNode {
private ArrayList<Integer> values = new ArrayList<Integer>();
private int maxSize;
public ListNode(int maxSize) {
this.maxSize = maxSize;
}
public ArrayList<Integer> getValues() {
return values;
}
public int getMaxSize() {
return maxSize;
}
public int getSize() {
return values.size();
}
public int getValueAt(int position) {
if ( position < values.size())
return values.get(position);
else
throw new IndexOutOfBoundsException();
}
public void addValue(int value) {
values.add(value);
}
public void removeValue(int position) {
if ( position < values.size()) {
values.remove(position);
} else
throw new IndexOutOfBoundsException();
}
}
}
Here we go. The trivial solution consist in extracting all the elements of each array node and store them in a single big array, sort that big array (using Bubble Sort, Quick Sort, Shell Sort, etc.) and finally reconstruct the linked list of arrays with the the sorted values. I am almost sure that is not exactly what are you looking for.
If you want to sort the numbers in place, I can think of the following algorithm:
As others have commented, you need a comparison function that determines if a node A goes before a node B. The following algorithm use the first idea but for each pair of nodes, e.g. A->[3, 9, 7] and B->[1, 6, 8] becomes [1, 3, 6, 7, 8, 9] and finally A->[1,3, 6] and B->[7, 8, 9]. If we apply this rearrangement for each possible pair will end up with a sorted linked list of arrays (I have no proof, though).
A = head;
while (A.hasNext()) {
arrayA = A.getArray();
B = A.getNext();
while (B.hasNext()) {
arrayB = B.getArray();
// concatenate two arrays
int[] C = new int[arrayA.size() + arrayB.size()];
int i;
for (i = 0; i < arrayA.size(); i++)
C[i] = arrayA[i];
for ( ; i < arrayA.size() + arrayB.size(); i++)
C[i] = arrayB[i-arrayA.size()];
// sort the new arrays using agains Bubble sort or any
// other method, or Arrays.sort()
Arrays.sort(C);
// now return the sorted values to the two arrays
for (i = 0; i < arrayA.size(); i++)
arrayA[i] = C[i];
for (i = 0; i < arrayB.size(); i++)
arrayB[i] = C[i+arrayA.size()];
}
}
This is kind of pseudo code because I haven't worked with linked lists in Java but I think you get the idea.
NOTE: I haven't tested the code, it may contain horrors.

Categories

Resources