bhaskarreddy
ARRAYS SORTING
2 Answer(s)      3 years and 5 months ago
Posted in : Java Interview Questions

How To Sort An Array With Out Using Sort Method ?I Want Code?
View Answers

January 3, 2010 at 1:06 AM


Hi,
Here is the code in java. You can find both Ascending and Descending order code. Ascending order is commented.

public class ArraySort {

public static void main(String[] args) {
int a[] = {6,500,700,200,1000,1};
int temp=0;
/*** Descending Order***/
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a.length;j++)
{
if(a[i]>a[j]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
/*** Ascending Order ***/
/*for(int i=0;i<a.length;i++)
{
for(int j=a.length-1;j>i;j--)
{
if(a[i]>a[j]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}*/
for(int i=0;i<a.length;i++)
System.out.println("a["+i+"] = "+a[i]);
}

}

Output:
a[0] = 1000
a[1] = 700
a[2] = 500
a[3] = 200
a[4] = 6
a[5] = 1

Regards,
javaquest2010.

January 4, 2010 at 10:51 AM


Hi Friend,

Try the following code:

public class SortArrayWithoutUSingMethod{
public static void sortArray(int array[], int len){
for (int i = 1; i < len; i++){
int j = i;
int tmp = array[i];
while ((j > 0) && (array[j-1] > tmp)){
array[j] = array[j-1];
j--;
}
array[j] = tmp;
}
}
public static void main(String a[]){
int i;
int array[]={7,2,9,6,4,8,3,1};
sortArray(array, array.length);
System.out.println("Sorted Array is: ");
for(i = 0; i <array.length; i++){
System.out.println(array[i]);
}
}

}

Thanks









Related Pages:
Sorting
Sorting  can any help me know which sorting algorithm java uses for sorting collection and arrays
merge sorting in arrays - Java Beginners
merge sorting in arrays  Write a program to insert string or characters to an array and apply merge sorting on this array  Hi Friend, Please visit the following link: http://www.roseindia.net/java/beginners
ARRAYS SORTING - Java Interview Questions
ARRAYS SORTING  How To Sort An Array With Out Using Sort Method ?I Want Code?  Hi, Here is the code in java. You can find both Ascending and Descending order code. Ascending order is commented. public class
Sorting String arrays in java - Java Beginners
Sorting String arrays in java  I have to make an interface... and make appropriate calls to interface to store and sort the arrays. I do not think that my sorting thusfar is correct. Can anyone help? Please help
PHP Sorting Arrays Tutorial
PHP Sorting Arrays       Sorting an array is the most useful thing you can do with an array. We...;jaguar","land rover"); echo "<b>Before sorting
Sorting Arrays
sorted based on a key. Sorting Arrays with Arrays.sort(...) The java.util.Arrays class contains a number of static methods for sorting arrays, both arrays.... Example - Sorting arrays using Arrays.sort() This example sorts an array of Strings
Sorting the array
Sorting the array  Implement a program to process votes for 5 candidates in a talent contest. The program should use a String array to hold the names... who was in 3rd, 2nd and 1st place: Hints: ? you will need to declare 2 arrays
sorting
sorting   write a program to arrange sorting in rows,column and diagonal
sorting
sorting  write to a program to arrange the sorting in rows, column and digonal
Array sorting - Java Beginners
Array sorting   Hello All. I need to sort one array based on the arrangement of another array. I fetch two arrays from somewhere and they are related. For example, String type[] = {"X","T","3","R","9"}; String
sorting
sorting  how to do sorting without using bubble sort,selection sort
arrays help - Java Beginners
arrays help  Write a program that sorts values. ? The program includes readIntegers() that o reads the user inputs o stores them in an array...  Easy Sorting: For all the values in the array A, find the largest and store
arrays
arrays  using arrays in methods   Java use arrays in methods import java.util.*; class ArrayExample{ public static int getMaxValue(int[] arr){ int maxValue = arr[0]; for(int i=1;i < arr.length;i
arrays
Store a table with students and information (name, ID, password, crypted password, etc) in a multi-dimensional array "stud"  Arrays and Strings: Store a table with students and information (name, ID, password, crypted password
Arrays
Arrays   Hi I need help with the following exercises. Exercise 1: Write a Java application in which the user is prompted for the total number of integer values to be stored in an array. Initialize the array with random values
PHP Array Sorting
sorting the values are:</b>"; print_r($car); sort($car); echo"<br/>"; echo "<b>After sorting values become:</b>"; print_r($car); ?> Output: Before sorting the values are:Array
Java using arrays - Java Beginners
("Sorting numbers"); for(int i=0;i<10;i++) { System.out.println
Sorting in Java
Sorting in Java  Sorting in Java
Java Arrays Tutorial
Java Arrays Tutorial       Learn Java Arrays from scratch with ease... download. Introduction to Java Arrays In this section you
group by sorting
group by sorting  group by sorting of data in SQL
linear sorting
linear sorting  what is linear sorting? can any send me an example   Hi Friend, Try this: import java.util.*; class ArraySort{ public...("After Sorting: "); for(int i=0;i<arr.length;i++){ System.out.println
Arrays in java
Arrays in java  what is param array in java
Sorting Program
Sorting Program  To sort 10items in alphabetical order   Arrays.sort(name of ur array)   import java.util.*; class ArrayExample{ public static void main(String[] args) { String array[]=new
sorting numbers
sorting numbers  How to sort the numbers in ascending order   import java.util.*; class SortNumbers{ public static void main(String[] args) { Scanner input=new Scanner(System.in
Java sorting
sorting mechanism. say about Collections.Sort() & Arrays.Sort() that uses
sorting and storing data
sorting and storing data   sorting and storing data in UITableView
Sorting and Searching
Sorting and Searching  Hi i would really be thankful if someone could help me with this A program is required to ask users to rate the Java programming language using a number in the range 0 - 10. The data input is ended
Sorting an ArrayList
Sorting an ArrayList  print("code sample");Hello All, I am working on a program for school and I am having trouble with sorting my list. The numbers I receive in my output are correct, but not in ascending order as required
java arrays
java arrays  can i know how can we intilize the three dimentional arrays in java? and how can we assign thae values to that array
java arrays
java arrays  i need a java program to store student details like id,name,addr,marks,average,total..using arrays..input data using scanner class and by using class, object and constructor
Java with Arrays
Java with Arrays  I was given the assignment to create two parallel arrays; both of size 50 (declares 50 as a constant) One is an array of strings... and store it in the arrays. The input for the problem should be provided in a text
java arrays
java arrays  how do you write the code for multipliying 2 double arrays to be in a 3rd double array? here is my code: package employeepay; /** * * @author Owner */ public class Main { /** * @param args the command line

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.