Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


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


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

Advertisement
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 Tutorials/Questions & Answers:
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
Advertisements
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
Sorting
Sorting  can any help me know which sorting algorithm java uses for sorting collection and arrays
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
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
sorting
sorting  how to do sorting without using bubble sort,selection sort
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
Sorting in Java
Sorting in Java  Sorting in Java
group by sorting
group by sorting  group by sorting of data in SQL
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
Arrays in java
Arrays in java  what is param array in java
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
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
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
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
Bubble Sorting in Java
Bubble Sorting in Java  Hi, What is Bubble Sorting? Guide me where to learn Bubble Sorting in Java? Thanks   Hi, Check the tutorial at Bubble Sorting in Java. Thanks
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 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 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
Advertisements
 

 

 

DMCA.com