How To Sort An Array With Out Using Sort Method ?I Want Code?
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 Beginnersmerge
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 QuestionsARRAYS 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 BeginnersSorting 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
SortingSorting 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
sortingsorting write to a program to arrange the
sorting in rows, column and digonal
sortingsorting how to do
sorting without using bubble sort,selection sort
arraysarrays 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
arraysStore 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 the arraySorting 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 sortinglinear 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 ProgramSorting 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 numberssorting 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 arraysjava 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 arraysjava 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 and SearchingSorting 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 ArraysJava 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 ArrayListSorting 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 JavaBubble
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
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 arraysjava 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 Beginnersarrays 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