Insert, Delete, Search, Print an int Array in Java
By: Grant Braught
The sample java program demonstrates sorting an int array. This class contains static methods which implement a number of classic sorting algorithms including: Insertion Sort, Selection Sort and Merge Sort.public class Sorts {
/**
* Method which sorts the array referred to
* by a using the insertion sort algorithm.
*
* @param a the array to be sorted.
*/
public static void insertionSort(int[] a) {
// For each element in the array of integers...
// (Note: the first element will not need to be considered
// because it is already in order with respect to itself!)
for (int loc=1; loc= 0 && a[i] > a[i+1]) {
int tmp = a[i];
a[i] = a[i+1];
a[i+1] = tmp;
i--;
}
}
}
/**
* Method which sorts the array referred to
* by a using the selection sort algorithm.
*
* @param a the array to be sorted.
*/
public static void selectionSort(int[] a) {
// For each element in the array of integers...
// (Note: the final element will not need to be considered
// because by the the time it would be considered the array
// will already be correctly sorted.)
for(int loc=0; loc= left.length) {
// The left array has been use up...
rest = right;
restIndex = rightIndex;
}
else {
// The right array has been used up...
rest = left;
restIndex = leftIndex;
}
// Copy the rest of whichever array (left or right) was
// not used up.
for (int i=restIndex; i
Archived Comments
1. can you guys please help me in inserting values on specific user defined index
View Tutorial By: Fariha at 2015-06-15 20:44:04
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Java program to get location meta data from an image
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program