Tech Rocks

Coldfusion
Java
JQuery

An online resource for latest web technologies like Coldfusion, JRun, Pro*C, JQuery, HTML5, PHP, W3C, Java, J2EE, C, C++, ORACLE, PL/SQL, MySql, Ajax, Coldbox, Fusebox, UNIX, JavaScript, NodeJS and much more... contact@tech-rocks.org

Wednesday, February 26, 2014

Selection sort in java

The  first iteration will give the smallest number at the first end of the list. The elements starting from the first is compared / swapped with the rest of the positions.

N-1 iterations for N items. Time complexity is O(n2)

public class MySelectionSort {

public static int[] doSelectionSort(int[] arr){

for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[index])
index = j;

int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
return arr;
}

public static void main(String a[]){

int[] arr1 = {10,34,2,56,7,67,88,42};
int[] arr2 = doSelectionSort(arr1);
for(int i:arr2){
System.out.print(i);
System.out.print(", ");
}
}
}


Output will be like



2, 7, 10, 34, 42, 56, 67, 88, 

Insertion sort in java

Time complexity is O(n2)
public class MyInsertionSort {

public static void main(String[] args) {

int[] input = { 4, 2, 9, 6, 23, 12, 34, 0, 1 };
insertionSort(input);
}

private static void printNumbers(int[] input) {

for (int i = 0; i < input.length; i++) {
System.out.print(input[i] + ", ");
}
System.out.println("\n");
}

public static void insertionSort(int array[]) {
int n = array.length;
for (int j = 1; j < n; j++) {
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ) ) {
array [i+1] = array [i];
i--;
}
array[i+1] = key;
printNumbers(array);
}
}
}


Output will be like:



2, 4, 9, 6, 23, 12, 34, 0, 1, 

2, 4, 9, 6, 23, 12, 34, 0, 1,

2, 4, 6, 9, 23, 12, 34, 0, 1,

2, 4, 6, 9, 23, 12, 34, 0, 1,

2, 4, 6, 9, 12, 23, 34, 0, 1,

2, 4, 6, 9, 12, 23, 34, 0, 1,

0, 2, 4, 6, 9, 12, 23, 34, 1,

0, 1, 2, 4, 6, 9, 12, 23, 34,

Bubble sort in java

The logic is to swap the 2 items in the list during iterations. At the end of first iteration the list will have the largest no. at the end.

n-1 iterations for n items. Time complexity is O(n2)

import java.util.Scanner;

class BubbleSort {
public static void main(String []args) {
int n, c, d, swap;
Scanner in = new Scanner(System.in);

System.out.println("Input number of integers to sort");
n = in.nextInt();

int array[] = new int[n];

System.out.println("Enter " + n + " integers");

for (c = 0; c < n; c++)
array[c] = in.nextInt();

for (c = 0; c < ( n - 1 ); c++) {
for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d+1]) /* For descending order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}

System.out.println("Sorted list of numbers");

for (c = 0; c < n; c++)
System.out.println(array[c]);
}
}


Output will be like:



op

Friday, February 21, 2014

IndexOf Logic in Java

Logic of the following function:

IndexOf(String,Sub String) – Gives the position of the first occurrence of the substring.

Please check and comment.

import java.io.IOException;
import javax.servlet.http.*;

@SuppressWarnings("serial")
public class TestIndexOf extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
resp.getWriter().println("String IndexOf Implementation...");

resp.getWriter().println("Pos = " + IndexOf("Hello Jeetu", "llo"));

}

public int IndexOf(String main, String sub) {

char[] mainc = main.toCharArray();
char[] subc = sub.toCharArray();

for (int i = 0; i < mainc.length; i++) {
if (mainc[i] == subc[0]) {
for (int j = 0; j < subc.length; j++) {
if (mainc[i + j] != subc[j])
break;
else if (j == subc.length - 1)
return i + 1;
}

}
}
return -1;
}
}