Posts

Showing posts from September, 2015

Check if strings are rotations of each other or not - Java

public class RotatedOrNot { public boolean isRotated (String realString , String stringToTest){ String helpingString = realString+realString ; if (helpingString.contains(stringToTest)) return true; else return false; } } public class RotatedOrNotTest { @Test public void testNormalCase () { RotatedOrNot rotatedOrNot = new RotatedOrNot() ; assertEquals ( true, rotatedOrNot.isRotated( "abcde" , "cdeab" )) ; } }

Clustered Index vs non clustered index

With a clustered index the rows are stored physically on the disk in the same order as the index. There can therefore be only one clustered index. With a non clustered index there is a second list that has pointers to the physical rows. You can have many non clustered indexes, although each new index will increase the time it takes to write new records. It is generally faster to read from a clustered index if you want to get back all the columns. You do not have to go first to the index and then to the table. Writing to a table with a clustered index can be slower, if there is a need to rearrange the data.

GET vs POST

To Create the resource - POST To Get the resource - GET In ReST: Create - POST Read - GET Update - PUT Delete - DELETE

Bubble sort - Java

public class BubbleSort { public static int [] bubbleSort ( int [] a){ for ( int i = 0 ; i < a. length - 1 ; i++) { for ( int j = 1 ; j < a. length - i ; j++) { if (a[j- 1 ] > a[j]){ int tmp = a[j- 1 ] ; a[j- 1 ] = a[j] ; a[j] = tmp ; } } System. out .println(i+ 1 + "iteration:" + Arrays. toString (a)) ; } return a ; } public static void main (String[] args) { int [] a = new int [] { 4 , 2 , 7 , 1 , 3 , 8 } ; System. out .println(Arrays. toString ( bubbleSort (a))) ; } } o/p: 1iteration:[2, 4, 1, 3, 7, 8] 2iteration:[2, 1, 3, 4, 7, 8] 3iteration:[1, 2, 3, 4, 7, 8] 4iteration:[1, 2, 3, 4, 7, 8] 5iteration:[1, 2, 3, 4, 7, 8] [1, 2, 3, 4, 7, 8]

Merge sort -.Java

public class MergeSort { public static void mergeSort ( int [] a , int low , int high){ int N = high - low ; if (N<= 1 ) return; int mid = low + N/ 2 ; mergeSort (a , low , mid ) ; mergeSort (a , mid , high) ; int temp[] = new int [N] ; int i = low ; int j = mid ; for ( int k = 0 ; k < N ; k++) { if (i == mid ) temp[k] = a[j++] ; else if (j == high) temp[k] = a[i++] ; else if (a[j] < a[i]) temp[k] = a[j++] ; else temp[k] = a[i++] ; } for ( int k = 0 ; k < N ; k++) { a[low + k] = temp[k] ; } } public static void main (String[] args) { int [] a = new int [] { 2 , 6 , 3 , 7 , 1 , 9 , 4 } ; mergeSort (a , 0 , a. length ) ; for ( int i = 0 ; i < a. length ; i++) { System. out .print...