1 Replies - 213 Views - Last Post: 15 March 2010 - 01:23 PM Rate Topic: -----

#1 Guest_duny*


Reputation:

display output

Posted 15 March 2010 - 10:35 AM

below is my code...all works fine and compile ok...but the selection sort wont display any output...pls i need help.....???






/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package capreparation2;

import java.util.Arrays;

/**
 *
 * @author student
 */
public class CA2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic her


            System.out.print("QUESTION 1" + "\n");

            
        int[] array1 = new int [5];
        int[] array2 = new int [5];


        array1[0] = 2;
         array1[1] = 4;
          array1[2] = 6;
           array1[3] = 8;
            array1[4] = 10;

            //System.out.print(array1[3] + "\n");

        array2[0] = 3;
         array2[1] = 5;
          array2[2] = 7;
           array2[3] = 9;
            array2[4] = 11;

           // System.out.print(array2[2] + "\n");

  int[] array3 = new int [array1.length + array2.length];//3rd array to hold the merged arrays
        // OR int[] array3 = new int [10];//3rd array to hold the merged arrays

      for(int i = 0; i < array3.length; i++)
      {
         if(i < array1.length)
         {
             array3[i] = array1[i];

         }else

             array3[i] = array2[i - array1.length];

            // System.out.print(array3[i]);
         //System.out.print(array3[i]);
      }
  Arrays.sort(array3);

    for(int i = 0 ; i < array3.length; i++)
    {
    System.out.println(array3[i] + " ");
    }

System.out.print("QUESTION 2" + "\n");

                                 //QUESTION 2


  int[] arr = {3,6,7,4,5,9,2};
  int index = 3; // index of the array where u want the split to take place.

        int[] a1 = new int[4];
        int[] a2 = new int[3];

    for(int i = 0; i < a1.length; i++)
    {
        a1[i] = arr[i];
    }

    for(int i = 0; i < a2.length; i++)
    {
        a2[i] = arr[index + i +1];
    }

        // Print array 1.
  System.out.print("Array 1" + "\n");
for(int i = 0; i < a1.length; i++)
    {
    System.out.print(a1[i] + " " + "\n");
    }
        // Print array 2.
   System.out.print("Array 2" + "\n");
for(int i = 0; i < a2.length; i++)
    {
    System.out.print(a2[i] + " " + "\n");
    }

    }


public static void selectionSort1(int[] randomInteger)
{

     randomInteger = new int[8];

     randomInteger[0] = 12;
     randomInteger[1] = 7;
     randomInteger[2] = 8;
     randomInteger[3] = 4;
     randomInteger[4] = 2;
     randomInteger[5] = 6;
     randomInteger[6] = 16;
     randomInteger[7] = 1;



    for (int i=0; i < randomInteger.length-1; i++)
    {
        for (int j=i+1; j<randomInteger.length; j++)
        {
            if (randomInteger[i] > randomInteger[j])
            {
                //... Exchange elements
                int small = randomInteger[i];
                randomInteger[i] = randomInteger[j];
                randomInteger[j] = small;
            }
        }

    }
      for(int i = 0 ; i < randomInteger.length; i++)
    {
    System.out.println(randomInteger[i] + " ");
    }
   
}


 
    
                              
}




Is This A Good Question/Topic? 0

Replies To: display output

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: display output

Posted 15 March 2010 - 01:23 PM

You never call the selectionSort1 method from your main method :)

System.out.println("Selection Sort!");
selectionSort1(new int[1]);


It actually doesn't matter what you pass as a parameter to the method, since it creates a new array and assigns values to it, so I just did new int[1]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1