Click here to Skip to main content
15,897,334 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello all ^_^
I want to sort an (one dimension array) using the following way only.
The way is :

We have this array :
Index : 0-----1-----2-----3-----4-----5-----6
Values: 10----5-----6-----11----2-----5-----1

2- Create a new array :
Java
int []Sorting =new int [7] 


3- now we want to find min number from the source array the put it in index 0 in the new array "Sorting" then make the min number in source array = 0 , like that :
this is the source array after one loop :
Index : 0-----1-----2-----3-----4-----5-----6
Values: 10----5-----6-----11----2-----5-----0
this is the sorted array :

Index : 0-----1-----2-----3-----4-----5-----6
Values: 1----null---null--null--null--null--null

4- We must loop this step to make "Sorting"array sorted, like this :

Index : 0-----1-----2-----3-----4-----5-----6
Values: 1-----2-----5-----5----6-----10-----11
======================================================================================
this is my source code :


Java
public static void main(String[] args) 
    {   

int [] RandomArray = new int [8];
    //Inserting
    for(int i=0;i<RandomArray.length;i++)
    {

String element= JOptionPane.showInputDialog("Please insert any number "+ (i+1));
RandomArray [i]=Integer.parseInt(element);


    }

    int []SortedArray=new int [8];
    for(int s=0; s< RandomArray.length;s++)
    {


    for(int d=0;d<RandomArray.length;d++)
    {



            if (s==d || RandomArray[s]==0) 
        {


        }
        else



        {


            int x ;

            if (RandomArray[s]<RandomArray[d])

            {

                //SortedArray[s] = RandomArray[s];
                SortedArray[s] = RandomArray[s];
                x=s;

            }else

            {
                SortedArray[s] =    RandomArray[d];
                x=d;
            }

            RandomArray[x]=0;


        }//end first else
    }//end second for


    }//end first for

}






please help me quickly.
Posted
Updated 30-Jun-14 1:07am
v2
Comments
OriginalGriff 30-Jun-14 7:27am    
And?
What help do you need?
What does it do that you didn't expect, or not do that you did?
Anas T.P.M 30-Jun-14 11:50am    
this code dosen't work to give me the sorted array ..I put this code here to suggest an edit to me about how to make this code work done.
OriginalGriff 30-Jun-14 12:06pm    
OK.
So why doesn't it work?
What have you done to check if it does? What happens when you try to debug it?
This is your homework, so you should probably be involved in the fix...:laugh:

1 solution

That is a selection sort and works this way (pseudocode)
Java
for i=0, a.length-1
{
 j = index_of_min(a, i)
 swap(a, i, j )
}


where
  • index_of_min(a,i) finds the index of the smallest value in the subarray of a starting from index i.
  • swap(a,i,j) swaps the items i, j of the array a.
 
Share this answer
 
Comments
T.P.M 1-Jul-14 4:50am    
please explain you solution , I didn't understand it .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900