ARRAYS SORTING

ARRAYS SORTING

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 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...;land rover");ADS_TO_REPLACE_5 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
Arrays
called Rebel.java; 2.Create two arrays. One of them must store integer numbers... the numbers in the second array by 3; 7.Print out the contents of both arrays. 8.Swap
Sorting in Java
Sorting in Java  Sorting in Java
group by sorting
group by sorting  group by sorting of data in SQL
Arrays in java
Arrays in java  what is param array in java
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
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 and storing data
sorting and storing data   sorting and storing data in UITableView
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
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
sorting
sorting
sorting
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
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
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
arrays
Arrays
Arrays
arrays
arrays
Arrays
arrays
arrays
arrays
Arrays
arrays
arrays
arrays
create arrays in JavaScript
create arrays in JavaScript  How to create arrays in JavaScript
How to create arrays in JavaScript?
How to create arrays in JavaScript?  How to create arrays in JavaScript
Comparing arrays not working correctly?
Comparing arrays not working correctly?  Comparing arrays not working correctly
Compare two Byte Arrays?
Compare two Byte Arrays?  Compare two Byte Arrays
ModuleNotFoundError: No module named 'sorting'
ModuleNotFoundError: No module named 'sorting'  Hi, My Python... 'sorting' How to remove the ModuleNotFoundError: No module named 'sorting... to install padas library. You can install sorting python with following command
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 {ADS_TO_REPLACE_1 /** * @param args
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

Ads