John Delmark Aguilar

Delmark - Profiling
 
HomeCalendarFAQSearchMemberlistUsergroupsRegisterLog in

Share | 
 

 Data Sorting: InsertionSort Java

View previous topic View next topic Go down 
AuthorMessage
dElmARk
Admin
Admin
avatar

Posts : 92
Join date : 09/04/2012

PostSubject: Data Sorting: InsertionSort Java   Tue Oct 23, 2012 6:06 am

InsertionSort Java

Code:

public class InsertionSort{
 public static void main(String args[])
 {
 
  int i;
  int array[]={12,9,4,99,120,1,3,10};
  System.out.println("Values Before the sort:");
  for(i=0; i<array.length; i++)
    System.out.print(array[i]+" ");
    System.out.println();
    insertion_srt(array, array.length);
    System.out.print("Values after the sort:\n");
    for(i=0; i<array.length; i++)
      System.out.print(array[i]+" ");
      System.out.println();
      System.out.println("End of the sort");
 }
 public static void insertion_srt(int array[], int n)
 {
  for(int i=1; i<n; i++)
  {
    int j=i;
    int B=array[i];
    while((j>0)&&(array[j-1]>B))
    {
    array[j]=array[j-1];
    j--;
    }
    array[j]=B;
  }
    }
}

Activity:
Create a program that will interact with the user.

The program must do the following:
1. Ask the user "How many numbers will be sorted? "
2. Display the unsorted numbers.
3. Display the sorted numbers in ascending order.
4. Display the sorted numbers in descending order.
Back to top Go down
View user profile http://delmark.meabout.com
 
Data Sorting: InsertionSort Java
View previous topic View next topic Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
John Delmark Aguilar :: dElmARk Productions :: Java Programs :: Tutorials-
Jump to: