Uncategorized

Selection Sort Java Program

Selection sort is a kind of sorting mechanism where every element is compared with the every other element to sort the elements.
The following programs performs a selection sort on integers and also also calculates number of iteration required for sorting entire array. And also it simulates how the array is actually sorted on each iteration.

 


package sorting;

import java.util.Arrays;

public class SelectionSort {

public static void main(String[] rawInput) {

int numberOfIterations = 0;
int length = rawInput.length;
int input[] = new int[length];
for (int k = 0; k < length; k++) {
input[k] = Integer.parseInt(rawInput[k]);
}

System.out.println("Raw input" + Arrays.toString(input));

if (length == 0) {
System.out.println("Invalid input ");
} else {
for (int i = 0; i < length; i++) {
numberOfIterations++;
// iterate the rest of the array
for (int j = i; j input[j]) {
int temp = input[i];
input[i] = input[j];
input[j] = temp;
System.out.println(Arrays.toString(input));
}

}

}
}

System.out.println("after sorting" + Arrays.toString(input));
System.out.println("Total Number of iterations:" + numberOfIterations);

}

}

If we run the above program with following input,
Raw input[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[8, 9, 7, 6, 5, 4, 3, 2, 1, 0]
[7, 9, 8, 6, 5, 4, 3, 2, 1, 0]
[6, 9, 8, 7, 5, 4, 3, 2, 1, 0]
[5, 9, 8, 7, 6, 4, 3, 2, 1, 0]
[4, 9, 8, 7, 6, 5, 3, 2, 1, 0]
[3, 9, 8, 7, 6, 5, 4, 2, 1, 0]
[2, 9, 8, 7, 6, 5, 4, 3, 1, 0]
[1, 9, 8, 7, 6, 5, 4, 3, 2, 0]
[0, 9, 8, 7, 6, 5, 4, 3, 2, 1]
[0, 8, 9, 7, 6, 5, 4, 3, 2, 1]
[0, 7, 9, 8, 6, 5, 4, 3, 2, 1]
[0, 6, 9, 8, 7, 5, 4, 3, 2, 1]
[0, 5, 9, 8, 7, 6, 4, 3, 2, 1]
[0, 4, 9, 8, 7, 6, 5, 3, 2, 1]
[0, 3, 9, 8, 7, 6, 5, 4, 2, 1]
[0, 2, 9, 8, 7, 6, 5, 4, 3, 1]
[0, 1, 9, 8, 7, 6, 5, 4, 3, 2]
[0, 1, 8, 9, 7, 6, 5, 4, 3, 2]
[0, 1, 7, 9, 8, 6, 5, 4, 3, 2]
[0, 1, 6, 9, 8, 7, 5, 4, 3, 2]
[0, 1, 5, 9, 8, 7, 6, 4, 3, 2]
[0, 1, 4, 9, 8, 7, 6, 5, 3, 2]
[0, 1, 3, 9, 8, 7, 6, 5, 4, 2]
[0, 1, 2, 9, 8, 7, 6, 5, 4, 3]
[0, 1, 2, 8, 9, 7, 6, 5, 4, 3]
[0, 1, 2, 7, 9, 8, 6, 5, 4, 3]
[0, 1, 2, 6, 9, 8, 7, 5, 4, 3]
[0, 1, 2, 5, 9, 8, 7, 6, 4, 3]
[0, 1, 2, 4, 9, 8, 7, 6, 5, 3]
[0, 1, 2, 3, 9, 8, 7, 6, 5, 4]
[0, 1, 2, 3, 8, 9, 7, 6, 5, 4]
[0, 1, 2, 3, 7, 9, 8, 6, 5, 4]
[0, 1, 2, 3, 6, 9, 8, 7, 5, 4]
[0, 1, 2, 3, 5, 9, 8, 7, 6, 4]
[0, 1, 2, 3, 4, 9, 8, 7, 6, 5]
[0, 1, 2, 3, 4, 8, 9, 7, 6, 5]
[0, 1, 2, 3, 4, 7, 9, 8, 6, 5]
[0, 1, 2, 3, 4, 6, 9, 8, 7, 5]
[0, 1, 2, 3, 4, 5, 9, 8, 7, 6]
[0, 1, 2, 3, 4, 5, 8, 9, 7, 6]
[0, 1, 2, 3, 4, 5, 7, 9, 8, 6]
[0, 1, 2, 3, 4, 5, 6, 9, 8, 7]
[0, 1, 2, 3, 4, 5, 6, 8, 9, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 9, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
after sorting[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Total Number of iterations:65
Uncategorized

Queue with O(1) complexity and unlimited size

The queue which works with First In First Out (FIFO) mechanism. This is the queue program in java with insertion complexity of O(1) in java.

/**
*
*/
package java8pract.datastructures;

import java.util.ArrayList;
import java.util.List;

/**
* @author prabhukvn
*
*/
public class CustomQueue<T extends Comparable> {
Node firstNode;
Node lastNode;
Node tempNode;
int stackSize = 0;

public void push(T element) {
Node node = null;
if (null == firstNode) {
node = new Node(null, element, null);
firstNode = node;
stackSize++;

} else {

Node middleNode = tempNode;
node = new Node(middleNode, element, null);
middleNode.next = node;
lastNode = node;
stackSize++;
}
tempNode = node;

}

public int getSize() {
return this.stackSize;
}

public T pop() {

T element = firstNode.element;
firstNode = firstNode.next;
firstNode.previous=null;
stackSize--;
return element;

}

/**
* Get all the elements in linked list
*
* @return
*/
public List<Node> getAll() {
List<Node> list = new ArrayList();
Node temp = firstNode;
while (temp != null) {
list.add(temp);
System.out.println(temp.element);
temp = temp.next;

}
return list;
}

}

Uncategorized

Stack with O(1) push complexity

This is a stack program where the algorithm works on Last in First Out (LIFO) concept. This is a typical stack program with insertion complexity as O(1) i.e constant.


/**
*
*/
package java8pract.datastructures;

import java.util.ArrayList;
import java.util.List;

/**
* @author prabhukvn
*
*/
public class CustomStack<T extends Comparable> {
Node firstNode;
Node lastNode;
Node tempNode;
int stackSize = 0;

public void push(T element) {
Node node = null;
if (null == firstNode) {
node = new Node(null, element, null);
firstNode = node;
stackSize++;

} else {

Node middleNode = tempNode;
node = new Node(middleNode, element, null);
middleNode.next = node;
lastNode = node;
stackSize++;
}
tempNode = node;

}

public int getSize() {
return this.stackSize;
}

public T pop() {

T element = lastNode.element;
lastNode = lastNode.previous;
lastNode.next=null;
stackSize--;
return element;

}

/**
* Get all the elements in linked list
*
* @return
*/
public List<Node> getAll() {
List<Node> list = new ArrayList();
Node temp = firstNode;
while (temp != null) {
list.add(temp);
System.out.println(temp.element);
temp = temp.next;

}
return list;
}

}