Sort.java |
| /* Sorting class for Exercise 7 7/4/01 James Little */ package jlittle_ex8; import java.util.*; public class Sort{ private Word [] v; private Word w; private int count; private int totalwords; public Sort(Word [] v,int count, int totalwords){ this.v=v; this.count=count; this.totalwords=totalwords; } public void quicksort(int lo, int hi){ if( hi > lo ){ int left=lo, right=hi; int median=v[lo].getFreq(); while(right >= left) { while(v[left].getFreq() < median) left++; while(v[right].getFreq() > median) right--; if(left > right) break; Word temp=v[left]; v[left]=v[right]; v[right]=temp; //swap left++; right--; } quicksort( lo, right);// sort the small elements divide and conquer quicksort( left, hi); } }//quicksort public void lexiSort(int lo, int hi){ int posMin; Word temp; for(int fill=lo; fill<hi; fill++){ posMin=findMin(fill,hi); if(posMin != fill){ temp=v[fill]; v[fill]=v[posMin]; v[posMin]=temp; } } }//lexiSort public int findMin(int fill,int hi){ int possi=fill; for(int i = fill; i<hi; i++){ if(v[i].lexidex() < v[possi].lexidex()) possi=i; } return possi; } public Word getWord(int index){ return v[index]; } public void writeData(){ for(int z=0; z<count; z++){ int word=v[z].getWord(); if(word==13||word==32||word==10||word==9||word==11) // whitespace values retun a string System.out.println(z+" "+v[z].getString()+"\t"+Math.round(v[z].getFreq()*100.0/totalwords) +"\t"+v[z].lexidex()); else System.out.println(z+" "+v[z].getChar()+"\t"+Math.round(v[z].getFreq()*100.0/totalwords) +"\t"+v[z].lexidex()); } } }//sort |
James Little |