How to read a URL using Java?


  • Below program consists of steps to read data from the URL
package in.malliktalksjava;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/**
* @author malliktalksjava
*
*/
public class URLConnectionReader {

public static void main(String[] args) throws Exception {

URL oracle = new URL("http://malliktalksjava.in/");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}

Quick Sort Example Program in Java


package in.malliktalksjava;

/**
* @author malliktalksjava
*
* Sort the given array using QuickSort Algorithm
*/
public class QuickSortExample {

private int array[];
private int length;

/**
* @param args
*
* Main Method. Calls the sort method and print the output into
* console.
*/
public static void main(String args[]) {
QuickSortExample quickSorter = new QuickSortExample();
int[] input = { 15, 42, 14, 100, 85, 1, 13, 6, 9 };
quickSorter.sortArray(input);
for (int count : input) {
System.out.print(count);
System.out.print(” “);
}
}

/**
* @param inputArr
*
* Checks given array is empty or null, if not calls quickSort
* method.
*/
public void sortArray(int[] inputArr) {

if (inputArr == null || inputArr.length == 0) {
return;
}
this.array = inputArr;
length = inputArr.length;
quickSort(0, length – 1);
}

/**
* @param lowerIndex
* @param higherIndex
*
* Sorts the given array
*/
private void quickSort(int lowerIndex, int higherIndex) {

int temp1 = lowerIndex;
int temp2 = higherIndex;
// calculate pivot number, here pivot is middle index number
int pivot = array[lowerIndex + (higherIndex – lowerIndex) / 2];
// Divide into two arrays
while (temp1 <= temp2) {
/**
* In each iteration, we will identify a number from left side which
* is greater then the pivot value, and also we will identify a
* number from right side which is less then the pivot value. Once
* the search is done, then we exchange both numbers.
*/
while (array[temp1] < pivot) {
temp1++;
}
while (array[temp2] > pivot) {
temp2–;
}
if (temp1 <= temp2) {
exchangeNumbers(temp1, temp2);
// move index to next position on both sides
temp1++;
temp2–;
}
}
// call quickSort() method recursively
if (lowerIndex < temp2)
quickSort(lowerIndex, temp2);
if (temp1 < higherIndex)
quickSort(temp1, higherIndex);
}

/**
* @param param1
* @param param2
*
* Exchange the numbers and set to instance array variable
*/
private void exchangeNumbers(int param1, int param2) {
int temp = array[param1];
array[param1] = array[param2];
array[param2] = temp;
}
}

 

Other Useful Links:

Bubble Sort Example in JAVA

Selection Sort Example Program in JAVA

Insertion Sort Example program in JAVA

Merge Sort Example in Java

Selection Sort Example Program in Java


package in.malliktalksjava;

/**
* @author malliktalksjava
* @version 1.0
*
* Below class sorts the given array using SelectionSort
* mechanism and prints the output into console.
*/
public class SelectionSortExample {

/**
* @param args
*/
public static void main(String args[]){

int[] arr1 = {12,434,2,23,7,44,66,42};
int[] arr2 = doSelectionSort(arr1);
for(int var:arr2){
System.out.print(var);
System.out.print(“, “);
}
}

/**
* @param arr
* @return sorted array
*/
public static int[] doSelectionSort(int[] arr){

for (int count = 0; count < arr.length – 1; count++)
{
int index = count;
for (int count2 = count + 1; count2 < arr.length; count2++){
if (arr[count2] < arr[index]){
index = count2;
}
}

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

 

Other Useful Links:

Bubble Sort Example in JAVA

Insertion Sort Example program in JAVA

Quick Sort Example Program in Java

Merge Sort Example in Java