我昨天发过帖子,但是大家没明白意思, 我今天想知道答案,请大家帮忙看看
import java.io.*;
UnsrotList{
public int[] theList;
public int used;
private int counterA;
private int counterB;
private int counterC;
private int counterD; /* precondition: length is positive
* postcondition: theList is created as an empty array of the input length,
* where each slot is initialised to 0.
* Best and worst case: O(length) */
public UnsortedIntList()
{}
public UnsortedIntList(int length)
{
theList = new int[length];
used = 0;
for (int i=0; i<length; i++) theList[i] = 0;
counterA=0;
counterB=0;
counterC=0;
counterD=0;
}
public void setCounterA(int a){ counterA=a; }
public int getCounterA(){return counterA;}
public void setCounterB(int b){ counterB=b; }
public int getCounterB(){return counterB;}
public void setCounterC(int c){ counterC=c; }
public int getCounterC(){return counterC;}
public void setCounterD(int d){ counterD=d; }
public int getCounterD(){return counterD;}
/* precondition: none new
* postcondition: returns true if theList is empty, false otherwise
* Best and worst case: O(1) */
public boolean isEmpty()
{return (used == 0);}
public boolean isFull()
{return (used == theList.length);} )
public boolean addItem(int item)
{
boolean hasSpaceLeft =(used<theList.length);
if (hasSpaceLeft)
{
theList[used] = item;
used++;
}
return hasSpaceLeft;
}
/* precondition: none new
* postcondition: returns -1 if the item does not apper in the list, and
* i if the item appears for the first time in position i of the array
* Best case: O(1) (first item), worse case: O(N) (not there) */
public int findItem(int item)
{
for (int i=0 ; i < used; i++)
{
if (theList[i]==item)
{return i;}
}
return -1;
}
/* precondition: none new
* postcondition: returns true if item appears in the list and its leftmost
* occurrence has been eliminated, false if it does not appear.
* Best and worst case: O(N) */
public boolean deleteItem(int elem)
{
int index=findItem(elem);
boolean found = (index>=0);
if (found)
{
for (int i=index; i<(used-1); i++)
{theList[i]=theList[i+1];}
used--;
}
return (found);
} public String toString()
{
String temp = new String("");
for (int i = 0; i<used; i++)
{
temp += theList[i]+" ";
}
return temp;
}
public void swap(int one, int two)
{
int temp=theList[one];
theList[one]=theList[two];
theList[two]=temp;
}
public void bulleSort()throws Exception
{ queue theQ=new queue(4);
int a=0;
int b=0;
int d=0;
for(int = used-1; > 0; --)
{
for(int i = 0; i < ; i++)
{
if (theList[i]>theList[i+1])
{
swap(i, i+1);
theQ.append(a++);
counterA=theQ.serve()+1;
}
theQ.append(b++);
counterB=theQ.serve()+1;
}
theQ.append(d++);
counterD=theQ.serve()+1;
System.out.println(counterD+" "+counterB+" "+counterC+" "+counterA);
}
}
public void selectionSort() throws Exception
{ queue theQ=new queue(4);
int a=0;
for (int =0; <used-1; ++)
{
int minIndex = findMin();
swap(, minIndex);
theQ.append(a++);
counterA=theQ.serve()+1;
}
System.out.println(counterD+" "+counterB+" "+counterC+" "+counterA);
}
private int findMin(int )
{ queue theQ1=new queue(4);
int b=0;
int d=0;
int posmin=;
try{ for (int i=posmin+1; i<used; i++)
if(theList[i]<theList[posmin])
{ posmin=i;
theQ1.append(b++);
counterB=theQ1.serve()+1;
}
theQ1.append(d++);
counterD=theQ1.serve()+1;
}
catch(Exception e){};
return posmin;
}
public void insertionSort() throws Exception {
queue theQ=new queue(4);
theQ.reset();
int c1=0;
for(int =1;<used;++)
{
int temp=theList[];
int i=-1;
theList[i]=temp;
while(i>=0&&theList[i]>temp)
{ theList[i+1]=theList[i];
theQ.append(i);
counterB+=theQ.serve()+1;
i--;
}
theQ.append(c1++);
counterC+=theQ.serve()+1;
}
System.out.println( counterC+" "+ counterB+" "+ counterC+" "+ counterA);
}
/////////////////////////////////////////////////////////
public class termaiting
{
public static void main(String[] arg)
{
try{ UnsortedIntList list=new UnsortedIntList(5);
list.addItem(2);
list.addItem(4);
list.addItem(1);
list.addItem(5);
list.addItem(3);
System.out.println("Iteration | Comparisons | Copies | Swaps-------bubbleSort");
list.bulleSort();
UnsortedIntList list2=new UnsortedIntList(5);
list2.addItem(2);
list2.addItem(4);
list2.addItem(1);
list2.addItem(5);
list2.addItem(3);
System.out.println("Iteration | Comparisons | Copies | Swaps------selectionSort");
list2.selectionSort();
UnsortedIntList list3=new UnsortedIntList(5);
list3.addItem(2);
list3.addItem(4);
list3.addItem(1);
list3.addItem(5);
list3.addItem(3);
System.out.println("Iteration | Comparisons | Copies | Swaps------insertionSort");
list3.insertionSort();
}
catch( Exception e){};
}
}
/////////////////////////////////////
我还引用了一个QUEUE相信大家都明白
我想得到的结果
///////////////////////////////
Iteration | Comparisons | Copies | Swaps-------bubbleSort
1 4 0 2
2 7 0 4
3 9 0 4
4 10 0 4
Iteration | Comparisons | Copies | Swaps------selectionSort
1 1 0 4
Iteration | Comparisons | Copies | Swaps------insertionSort
10 0 10 0
////////////////////////////
只有bullesort是正确的其他的都不正确,我怎么错了,各位给指点一下
import java.io.*;
UnsrotList{
public int[] theList;
public int used;
private int counterA;
private int counterB;
private int counterC;
private int counterD; /* precondition: length is positive
* postcondition: theList is created as an empty array of the input length,
* where each slot is initialised to 0.
* Best and worst case: O(length) */
public UnsortedIntList()
{}
public UnsortedIntList(int length)
{
theList = new int[length];
used = 0;
for (int i=0; i<length; i++) theList[i] = 0;
counterA=0;
counterB=0;
counterC=0;
counterD=0;
}
public void setCounterA(int a){ counterA=a; }
public int getCounterA(){return counterA;}
public void setCounterB(int b){ counterB=b; }
public int getCounterB(){return counterB;}
public void setCounterC(int c){ counterC=c; }
public int getCounterC(){return counterC;}
public void setCounterD(int d){ counterD=d; }
public int getCounterD(){return counterD;}
/* precondition: none new
* postcondition: returns true if theList is empty, false otherwise
* Best and worst case: O(1) */
public boolean isEmpty()
{return (used == 0);}
public boolean isFull()
{return (used == theList.length);} )
public boolean addItem(int item)
{
boolean hasSpaceLeft =(used<theList.length);
if (hasSpaceLeft)
{
theList[used] = item;
used++;
}
return hasSpaceLeft;
}
/* precondition: none new
* postcondition: returns -1 if the item does not apper in the list, and
* i if the item appears for the first time in position i of the array
* Best case: O(1) (first item), worse case: O(N) (not there) */
public int findItem(int item)
{
for (int i=0 ; i < used; i++)
{
if (theList[i]==item)
{return i;}
}
return -1;
}
/* precondition: none new
* postcondition: returns true if item appears in the list and its leftmost
* occurrence has been eliminated, false if it does not appear.
* Best and worst case: O(N) */
public boolean deleteItem(int elem)
{
int index=findItem(elem);
boolean found = (index>=0);
if (found)
{
for (int i=index; i<(used-1); i++)
{theList[i]=theList[i+1];}
used--;
}
return (found);
} public String toString()
{
String temp = new String("");
for (int i = 0; i<used; i++)
{
temp += theList[i]+" ";
}
return temp;
}
public void swap(int one, int two)
{
int temp=theList[one];
theList[one]=theList[two];
theList[two]=temp;
}
public void bulleSort()throws Exception
{ queue theQ=new queue(4);
int a=0;
int b=0;
int d=0;
for(int = used-1; > 0; --)
{
for(int i = 0; i < ; i++)
{
if (theList[i]>theList[i+1])
{
swap(i, i+1);
theQ.append(a++);
counterA=theQ.serve()+1;
}
theQ.append(b++);
counterB=theQ.serve()+1;
}
theQ.append(d++);
counterD=theQ.serve()+1;
System.out.println(counterD+" "+counterB+" "+counterC+" "+counterA);
}
}
public void selectionSort() throws Exception
{ queue theQ=new queue(4);
int a=0;
for (int =0; <used-1; ++)
{
int minIndex = findMin();
swap(, minIndex);
theQ.append(a++);
counterA=theQ.serve()+1;
}
System.out.println(counterD+" "+counterB+" "+counterC+" "+counterA);
}
private int findMin(int )
{ queue theQ1=new queue(4);
int b=0;
int d=0;
int posmin=;
try{ for (int i=posmin+1; i<used; i++)
if(theList[i]<theList[posmin])
{ posmin=i;
theQ1.append(b++);
counterB=theQ1.serve()+1;
}
theQ1.append(d++);
counterD=theQ1.serve()+1;
}
catch(Exception e){};
return posmin;
}
public void insertionSort() throws Exception {
queue theQ=new queue(4);
theQ.reset();
int c1=0;
for(int =1;<used;++)
{
int temp=theList[];
int i=-1;
theList[i]=temp;
while(i>=0&&theList[i]>temp)
{ theList[i+1]=theList[i];
theQ.append(i);
counterB+=theQ.serve()+1;
i--;
}
theQ.append(c1++);
counterC+=theQ.serve()+1;
}
System.out.println( counterC+" "+ counterB+" "+ counterC+" "+ counterA);
}
/////////////////////////////////////////////////////////
public class termaiting
{
public static void main(String[] arg)
{
try{ UnsortedIntList list=new UnsortedIntList(5);
list.addItem(2);
list.addItem(4);
list.addItem(1);
list.addItem(5);
list.addItem(3);
System.out.println("Iteration | Comparisons | Copies | Swaps-------bubbleSort");
list.bulleSort();
UnsortedIntList list2=new UnsortedIntList(5);
list2.addItem(2);
list2.addItem(4);
list2.addItem(1);
list2.addItem(5);
list2.addItem(3);
System.out.println("Iteration | Comparisons | Copies | Swaps------selectionSort");
list2.selectionSort();
UnsortedIntList list3=new UnsortedIntList(5);
list3.addItem(2);
list3.addItem(4);
list3.addItem(1);
list3.addItem(5);
list3.addItem(3);
System.out.println("Iteration | Comparisons | Copies | Swaps------insertionSort");
list3.insertionSort();
}
catch( Exception e){};
}
}
/////////////////////////////////////
我还引用了一个QUEUE相信大家都明白
我想得到的结果
///////////////////////////////
Iteration | Comparisons | Copies | Swaps-------bubbleSort
1 4 0 2
2 7 0 4
3 9 0 4
4 10 0 4
Iteration | Comparisons | Copies | Swaps------selectionSort
1 1 0 4
Iteration | Comparisons | Copies | Swaps------insertionSort
10 0 10 0
////////////////////////////
只有bullesort是正确的其他的都不正确,我怎么错了,各位给指点一下
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货