# Algorithms in Java

Written By : Debasis Das (25-Jan-2015)

## Program to generate Excel Column Headers in Java

In a Mac Excel there are 16384 Columns per sheet with the starting column of index 0 being A and the last column header is XFD with index 16383 (N-1)
In this program we will generate the column header alphabets using a simple Java Program.

```import java.util.ArrayList;
import java.util.Arrays;

public static void main(String[] args) {
String [] alphabets = new String[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};

ArrayList <String> newAlphabetArray = new ArrayList();
int endIndex = 16384; //The maximum number of columns available in Excel sheet
for (int i=0; i<endIndex; i++)
{
int quotient = i/26;
int remainder = i%26;
int secondLevelQuotient;
int secondLevelRemainder;

String alpha;
if (quotient == 0)
{
alpha = alphabets[remainder];
}
else if (quotient > 0 && quotient < 27)
{
alpha = alphabets[quotient-1] + alphabets[remainder];

}
else if (quotient >26)
{
secondLevelQuotient = quotient / 26;
secondLevelRemainder = quotient % 26;
if (secondLevelRemainder == 0)
{
alpha = alphabets[secondLevelQuotient-1]+alphabets[secondLevelRemainder]+alphabets[remainder];
}
else
{
alpha = alphabets[secondLevelQuotient-1]+alphabets[secondLevelRemainder-1]+alphabets[remainder];
}
}
}
System.out.println(newAlphabetArray.get(16383)); //Should print XFD
}
}
```

## Bubble Sort Algorithm in Java

```import java.util.Arrays;
public class BubbleSort {

public static void main(String[] args) {
int[] myIntArray = new int[]{120,200,301,543,234,19,87,67,54,37,89};
System.out.println(Arrays.toString(myIntArray));

//Begin Bubble Sort Algorithm
boolean dataExchanged = true;
int length = myIntArray.length - 1;
while ((length > 0) && (dataExchanged))
{
dataExchanged = false;
for (int i = 0; i< length; i++)
{
if (myIntArray[i] > myIntArray[i+1])
{
//Do Swap
int temp = myIntArray[i];
myIntArray[i] = myIntArray[i+1];
myIntArray[i+1] = temp;
dataExchanged = true;

}
}
length = length - 1;
}
//End Bubble Sort Algorithm
System.out.println(Arrays.toString(myIntArray));
}
}
```

## Selection Sort Algorithm in Java

```import java.util.Arrays;
public class SelectionSort {
public static void main(String[] args) {
int[] myIntArray = new int[]{120,200,301,543,234,19,87,67,54,37,89};
System.out.println("Before Sorting " + Arrays.toString(myIntArray));
//Begin Selection Sort Algorithm
int i,j;
int indexOfMin;
int length = myIntArray.length;
for (j=0; j < length - 1; j++)
{
//Assume that the first element is the minimum
indexOfMin = j;
//Test against elements after j to find a new smallest.
for (i = j+1; i< length; i++)
{
if (myIntArray[i] < myIntArray[indexOfMin])
{
//It means we have found a new minimum. We need to remember its index.
indexOfMin = i;
}
}
if (indexOfMin !=j)
{
//Do a swap
int temp = myIntArray[j];
myIntArray[j] = myIntArray[indexOfMin];
myIntArray[indexOfMin] = temp;
}
}
//End Selection Sort Algorithm
System.out.println("After Selection Sorting " + Arrays.toString(myIntArray));
}
}
```
Posted in algorithms, Java Tagged with: , , , ,
###### One comment on “Algorithms in Java”
1. Amrita says:

Well thought!