arrays class


/**
*
* @author ahmed farag
*/
public class SortElement {

/**
* method make sort of element
* idea of method  :-
* one we find max element and put at array.lenght-1
* then search max and swap
*
*/

private static void  mSortArray(int list[]){

for (int i=list.length-1;i>=1;i--){
//find max element
int maxCurrentElement=list[0];

//find index of max element
int indexOfCurrent=0;

for (int j=1;j<=i;j++){
if(maxCurrentElement<list[j]){
maxCurrentElement=list[j];
indexOfCurrent=j;
}//end of if

}//end of second for

// this part of we need to do swap
if(indexOfCurrent!=i){
list[indexOfCurrent]=list[i];
list[i]=maxCurrentElement;

}//end of if

}//end of first for

}//end of  sort method

/**
* method to print array element
*
* loop all array element
*/
private static void mPrintArrayElement (int list[]){

for (int i=0;i<list.length;i++){

System.out.println(i+":"+list[i]);

}//end of if

}//end of mehod

/**
* method print custom statment as ahin

*/

private static void mCustomPrinter(String Words){

System.out.println(Words);

}//end of method

public static void main(String []args){

int arrayToTesting[]={20,50,10,45,69,80,44,78};

//call my method and display results

SortElement ob=new SortElement();

ob.mCustomPrinter("before sort ");

ob.mPrintArrayElement(arrayToTesting);
ob.mCustomPrinter("after  sort ");

ob.mSortArray(arrayToTesting);

ob.mPrintArrayElement(arrayToTesting);

}//end of main

}//end of class

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s