Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Extra Storage Merge Sort in Java

                         

Introduction

In this example we are going to sort integer values of an array using extra storage merge sort.

In extra storage merge sorting algorithm  the unsorted values divide into two equal parts iteratively and create an array for store data value in extra storage. Then merge the two parts , sort it and store into an array .Then again merge the next part , sort it and store into an array. Do it iteratively
until  the values are not in sorted order. In this sorting the number of elements must be even. 

Code description:
In
extra storage  is similar to merge sort .But in extra storage merge sort the sorted values are stored in an other array.

Working of
extra storage merge sort algorithm:

Say we have an array unsorted  A[0],A[1],A[2]................ A[n-1] and A[n] as input. Then the following steps are followed by storage merge sort algorithm to sort the values of an array.

Step1:Spliting the values of array
Divide the values into two equal 1/2
A[0],A[1],A[2].........A[n/2-1]         &    A[n/2]....... .................A[n-1], A[n] 

 Again divide two equal 1/2
A[0] a[1]A[2]..............A[(n/2-1)/2-1] &  A[(n/2-1)/2]............A[n/2-1], 
A[n/2].............A[(2n-1)/2-1]  & a[(2n-1)/2].............A[n-1],A[n]
..........................................................................................................................
..........................................................................................................................
........................................................................................................................
A[0] & A[1] & A[2]& A[3],..............................................................A[n-1]& A[n]
 
Step2:Mergesets of  two values, sort the values and store into a different array 

A[0],A[1] & A[2],A[3]&..................................................................&A[n-1],A[n]
If A[1]<A[0],A[]<A[3]........................................................................A[n-1]>A[n]
then
A[1]A[0],A[2]A[3],...............................................................................A[n]A[n-1]
Step3:Merge sets of four values, sort the values and store into a different array
A[2] A[1] A[0] A[3],...................................................................................A[n-1]
..................................................................................................................
..................................................................................................................
.................................................................................................................
Step3:Merge n values, sort the values and store into a different array
A[2]A[6]......................................................................................................A[n-5]

Where n must be even number.

Steps of Merge Sort:

Say unsorted  an array values are:

12,9,4,99,120,1,3,10



The code of the program :

public class ExtraStorageMergeSort{
  public static void main(String a[]){
    int i;
    int array[] {12,9,4,99,120,1,3,10};
    int array1[] new int[array.length];
    System.out.println("\n\n       RoseIndia\n\n");
    System.out.println("       Extra Strorage Space Merge Sort\n\n");
    System.out.println("Values Before the sort:\n");
    for(i = 0; i < array.length; i++)
      System.out.printarray[i]+"  ");
    System.out.println();
    mergeSort_srt(array,0, array.length-1,array1);
    System.out.print("Values after the sort:\n");
    for(i = 0; i <array.length; i++)
    System.out.print(array1[i]+"  ");
    System.out.println();
    System.out.println("PAUSE");
    }
  
  public static void mergeSort_srt(int array[]int low, int high, int array1[]){
    if(low >= high) {
      return;
    }

    int middle = (low+high2;
    mergeSort_srt(array, low, middle, array1);
        mergeSort_srt(array, middle+1, high, array1);
        int k, t_low = low, t_high = middle+1;
        for(k = low; k <= high; k++)
            if ((t_low <= middle&& ((t_high > high|| (array[t_low
array
[t_high]))) {
        array1[k= array[t_low++];
      }
            else {
        array1[k= array[t_high++];
      }
    for(k = low; k <= high; k++) {
      array[k= array1[k];
    }
  }
}

Output of the example:

C:\array\sorting>javac ExtraStorageMergeSort.java
C:\array\sorting>java ExtraStorageMergeSort
       RoseIndia
       Extra Strorage Space Merge Sort
Values Before the sort:
12  9  4  99  120  1  3  10
Values after the sort:
1  3  4  9  10  12  99  120
PAUSE
C:\array\sorting>_

Download this example.

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Latest Searches:
how to get applicate s
jndi example
sleep
how to create to drop
log4j
PHP Date and .....:/pw
PHP Date and .....:/pw
check character/str.ph
servlets container
logout in jsp
array java
Insert Data into Datab
Javascript Randomizing
insert column
JDBC and Mysql
CMP
gwt ext tree structure
Treemap using gwt tree
shapes in applet
Photoshop Basics Draw
Java code for media pl
paramters
list in java
http get example
é???é????é????é???é???
form capture
input in array
Dreamweaver Behaviors
jdk-1.4.2
String to array in jav
breaké??å?¤?é?????é??å
changehashtablevalue
line break in javascri
two dimensional array
utility of collections
map iteration in jstl
JavaScript functions o
redirect
hashmap
break?ะ�ะ???????
how to increase the er
c tutorial
how to edit a any file
calling one jsp p
code for select date a
Generics
read a file
ะà¸??ะà¸
extract
close a frame
Move cursor to upper l
how to increase the er
numberStringsource
jsf comments
Copy One Database Tabl
example for adding the
html:frame
database path
decimal to binary conv
constuctor
Date in JSP
how to track user logi
character in javascrip
ล ยà¸?ฤ??????à¸
how to long progrm ja
Photoshop Drawing Awes
get data by using char
Declare tag methods in
javascript DB servl
iterate nest
program for display th
combo box field call a
tabbed pane
netbeans jsp
get array in java usin
sะà¸?ั?ยà¸?ะà¸
Creating Website with
save image to file
java code to connect t
File Validation code
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.