guys, is there any ways without using arrays.sort in ascending program...?
guys, is there any ways without using arrays.sort in ascending program...?
import java.util.*;
public class nnn
{
public static void main(String args[])
{
int characterArray[] = {5,2,4, 6, 1};
System.out.println("### Ascending ###");
for(int x = 0 ; x<characterArray.length; x ++)
{
System.out.println(characterArray[x]);
}
System.out.println("### Ascending ###");
System.out.println(characterArray[4]);// 1
System.out.println(characterArray[1]);//2
System.out.println(characterArray[2]);//4
System.out.println(characterArray[0]);//5
System.out.println(characterArray[3]);//3
}
}
with out using man daw sort..so, i come up with this idea....any idea u guys here about java ascending order without using sort?
^^ bro you're doing it manually. every time magchange ka sa entries sa imong array kay mag change pud ka sa order sa imong printlns. i guess nakahibalo naka ana. haha
i don't know how coz im a newbie for this program and im not used to it.....i do it manually gani kay im a newbie for this gani
how about transferring the data to another array, while transferring, kay naa siya'y mano2x nga sorting..?
i'll try to post code later..
There are many ways to sort, and each type has its own advantage and disadvantage but the most popular type and most preferred by everyone is merge sort. daghan codes available sa internet, naa gani available code sa wikipedia about merge sort. but if you want to learn how to code on your own (dba that's your goal?), get the algorithm of doing it then translate it to your code.
para dli ka ma tempt to see the code available everywhere, i copied the algorithm here,
Algorithm
Conceptually, a merge sort works as follows
1. If the list is of length 0 or 1, then it is already sorted. Otherwise:
2. Divide the unsorted list into two sublists of about half the size.
3. Sort each sublist recursively by re-applying merge sort.
4. Merge the two sublists back into one sorted list.
Merge sort incorporates two main ideas to improve its runtime:
1. A small list will take fewer steps to sort than a large list.
2. Fewer steps are required to construct a sorted list from two sorted lists than two unsorted lists. For example, you only have to traverse each list once if they're already sorted (see the merge function below for an example implementation).
In a simple pseudocode form, the algorithm could look something like this:
Following writing merge_sort function, then it is required to merge both the left and right lists created above. There are several variants for the merge() function; one possibility is this:Code:function merge_sort(m) if length(m) ≤ 1 return m var list left, right, result var integer middle = length(m) / 2 for each x in m up to middle add x to left for each x in m after middle add x to right left = merge_sort(left) right = merge_sort(right) result = merge(left, right) return result
Code:function merge(left,right) var list result while length(left) > 0 and length(right) > 0 if first(left) ≤ first(right) append first(left) to result left = rest(left) else append first(right) to result right = rest(right) end while if length(left) > 0 append left to result else append right to result return result
To picture it out:
File:Merge sort algorithm diagram.svg - Wikipedia, the free encyclopedia
A recursive merge sort algorithm used to sort an array of 7 integer values. These are the steps a human would take to emulate merge sort (top-down).
------------------------------------------
from Merge sort - Wikipedia, the free encyclopedia
Assignment ni noh? Well whoever asked u to do this probably meant for u to sort the array using your own code/logic and not use the existing sort method on java.util.Arrays class, right? So I would recommend u google/wiki bubble sort & implement it. There are other better, efficient sorting algo but this one is easier to implement.
//I'll be doing everything manually, at the same time use some advanced stuff as well that isn't related to sorting, comparators and the like.
//Point is no auto sort
//I have my own way of thinking, so don't state the obvious if there are better things to do...I'm helping not competing...
//If some may comment that I didn't use Iterators, well it is my personal preference that I don't like Iterators so buzz off...
//So if you are going to use this code, you just essentially use this by;
// yourArray = SampleSort.sort(yourArray);
// Kung naay questions or error please tell me.
// Lastly wala ni nako gicheck kung muwork...warning
public class SampleSort{
Integer[] newArray;
Vector<Integer> vector; //Check generics in java
private SampleSort(){ //private constructor you can't initialize it thru new
}
public static Integer[] sort(Integer[] array){
initialize();
scanElements();
transfer(); //Meaning transfer from vector to array, manually
return newArray;
}
private void initialize(){
newArray = new Integer[array.length];
vector = new Vector<Integer>() //Why use vectors? because they are easy to use.
}
private void scanElements(){
for(Integer i: array){ //One of the new ways of looping in Java
sortElement(i);
}
}
private void sortElement(Integer i){
ifEmptyAddDirectly(i); // If empty just add the first element directly
scanVector(i); // if has elements already proceed with comparisons
}
private void ifEmptyAddDirectly(Integer i){
if(vector.size() == 0){
vector.add(i);
}
}
private void scanVector(Integer i){
if(vector.size() == 0){
return;
}
boolean added = false; //placeholder if the element has been added to a vector
for(int x=0; x<vector.size(); x++){ //checking if current element is smaller
Integer element = vector.get(x);// from the top where it holds the smaller number
if( i <= element ){ // then onwards.
vector.add(x, i); // If element is smaller, proceed by replacing the currently compared number
return; // pushing all remaining elements down the list.
}
}
if(!added){ // If it wasn't added meaning it was larger than all number
vector.add(i); // proceed by adding it to the bottom
}
}
private void transfer(){
for(int x=0;x<vector.size();x++){
newArray[x] = vector.get(x);
}
}
}
Similar Threads |
|