Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

8.5 CODE LISTINGS

/*
  Selection Sort program
  Introducing Data Structures with Java
  David Cousins 2010
*/
import java.io.*;

public class SelectionSort {
   public static void main(String args[]) {
    long[] list = new long[20];
    long temp;
    int i;
    for (i = 0; i < 20; i++)
       list[i] = Math.round(Math.random()*200);
    int current, nextinorder, lowestindex;
    int start, limit;
    System.out.println(" Original unsorted list ");
    for (i = 0; i < 20; i++)
      System.out.print(list[i] + " ");
    start = 0;
    limit = 20;
    while (start < limit){
     nextinorder = start;
     lowestindex = start;
     for (current = start; current < limit; current++)
       if (list[current] < list[lowestindex])
          lowestindex = current;
     temp = list[nextinorder];
     list[nextinorder] = list[lowestindex];
     list[lowestindex] = temp;
     start++;
    }
    System.out.println();
    System.out.println(" Sorted list ");
    for (i = 0;i < 20;i++)
      System.out.print(list[i]+" ");
   }
  }
-----------------------------------------------------------------------
/*
  Bubble Sort program
  Introducing Data Structures with Java

					  

  David Cousins 2010
*/
import java.io.*;

public class BubbleSort {
 public static void sort(long[ ] values, int size){
   int i, n;
   long temp;
   int count = 0;                      // used to compare number of passes
   n = size − 1;
   while ((n> 0)){
    count++;
    for (i = 0; i < n; i++){
      if (values[i] > values[i + 1]) {
         temp = values[i];
         values[i] = values[i + 1];
         values[i + 1] = temp;
        }
       }
      n−−;
     }
     System.out.println("Number of passes is " + count);
   }
    public static void main(String args[]) throws IOException{
     int i;
     long[] list = new long[10];
     int i;
     for (i = 0; i < 10; i++)
         list[i] = Math.round(Math.random()*200);
     System.out.println("Unsorted list ");
     for (i=0;i<10;i++)
      System.out.print(list[i]+" ");
     System.out.println();
     sort(list, 10);
     System.out.println("Sorted list ");
     for (i = 0;i < 10;i++)
         System.out.print(list[i]+" ");
   }
 }
-----------------------------------------------------------------------
/*
  BubbleSort1 program
  a more efficient bubble sort − stops if list becomes sorted early
  Introducing Data Structures with Java
  David Cousins 2010
*/
  import java.io.*;
  public class Bubblesort1 {
   public static void sort(long[ ] values, int size){

					  


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint