Java中int数组链表的bubbleSort()
我对我独特的家庭作业采用泡泡运动法有困难 我们应该使用我们选择的排序方法来排序,得到一个int数组的链表。不是ArrayList,不仅仅是LinkedList。它的工作原理类似于链表,但每个节点包含一个容量为10整数的数组 我对分类方法一窍不通。我选择bubbleSort仅仅是因为它在上一次作业中使用过,而且我对它最熟悉。任何关于更好的排序方法的建议都会被认为是很有帮助的 这是我的密码:Java中int数组链表的bubbleSort(),java,arrays,Java,Arrays,我对我独特的家庭作业采用泡泡运动法有困难 我们应该使用我们选择的排序方法来排序,得到一个int数组的链表。不是ArrayList,不仅仅是LinkedList。它的工作原理类似于链表,但每个节点包含一个容量为10整数的数组 我对分类方法一窍不通。我选择bubbleSort仅仅是因为它在上一次作业中使用过,而且我对它最熟悉。任何关于更好的排序方法的建议都会被认为是很有帮助的 这是我的密码: public void bubbleSort() { current = head;
public void bubbleSort() {
current = head; // Start at the head ArrayNode
for (int i = 0; i < size; i++) { // iterate through each ArrayNode
currentArray = current.getArray(); // get the array in this ArrayNode
int in, out;
for (out = size-1; out > 1; out--) { // outer loop (backwards)
for (in = 0; in < out; in++) { // inner loop (forwards)
if (currentArray[in] > currentArray[in+1]) // out of order?
swap(in, in+1); // swap them!
}
}
current.setArray(currentArray);
current = current.getNext();
}
}// End bubbleSort() method
// A helper method for the bubble sort
private void swap(int one, int two) {
int temp = currentArray[one];
currentArray[one] = currentArray[two];
currentArray[two] = temp;
} // End swap() method
public void bubbleSort(){
current=head;//从head ArrayNode开始
对于(int i=0;i1;out--){//外部循环(向后)
for(in=0;incurrentArray[in+1])//出现故障?
交换(in,in+1);//交换它们!
}
}
current.setArray(currentArray);
current=current.getNext();
}
}//End bubbleSort()方法
//气泡排序的辅助方法
私有无效交换(整数1,整数2){
int temp=currentArray[1];
currentArray[1]=currentArray[2];
currentArray[two]=温度;
}//End swap()方法
这是我应该做的一个图片示例。
我们开始吧。简单的解决方案包括提取每个数组节点的所有元素并将它们存储在单个大数组中,对该大数组进行排序(使用冒泡排序、快速排序、Shell排序等),最后使用排序后的值重建数组的链表。我几乎可以肯定这不是你想要的 如果您想对数字进行适当排序,我可以想到以下算法: 正如其他人所评论的,您需要一个比较函数来确定节点a是否在节点B之前。以下算法使用第一种思想,但对于每对节点,例如
A->[3,9,7]
和B->[1,6,8]
变成[1,3,6,7,8,9]
,最后A->[1,3,6]
和B->[7,8,9]
。如果我们对每一个可能的对应用这种重新排列,最终将得到一个排序的数组链表(尽管我没有证据)
A=头部;
while(A.hasNext()){
arrayA=A.getArray();
B=A.getNext();
while(B.hasNext()){
arrayB=B.getArray();
//连接两个数组
int[]C=新的int[arrayA.size()+arrayB.size()];
int i;
对于(i=0;i
这是一种伪代码,因为我没有在Java中使用链表,但我想你已经明白了
注意:我还没有测试代码,它可能包含恐怖内容。我已经找到了selectionsort的解决方案。有一些测试值,只需运行它即可查看。 如果需要,我可以提供更多信息
import java.util.ArrayList;
import java.util.Random;
public class ArrayedListSort {
int listsize = 5; // how many nodes
int maxValue = 99; // the highest value (0 to this)
int nodeSize = 3; // size for every node
public static void main(String[] args) {
// run non static
new ArrayedListSort().runTest();
}
/**
* Log function.
*/
public void log(Object s) {
System.out.println(s);
}
public void logNoBR(Object s) {
System.out.print(s);
}
/**
* Output of list we have.
*/
public void logMyList(ArrayList<ListNode> listNode, String name) {
log("=== LOG OUTPUT " + name + " ===");
for ( int i=0; i < listNode.size(); i++) {
logNoBR(" node <" + i + ">");
logNoBR(" (");
for (int j=0; j < listNode.get(i).getSize(); j++) {
if ( j != (listNode.get(i).getSize()-1)) // if not last add ","
logNoBR( listNode.get(i).getValueAt(j) + "," );
else
logNoBR( listNode.get(i).getValueAt(j) );
}
log(")");
}
log("=====================================\n");
}
public void runTest() {
// create example List
ArrayList<ListNode> myList = new ArrayList<ListNode>();
// fill the nodes with random values
for ( int i = 0; i < listsize; i++) {
myList.add(new ListNode(nodeSize));
for (int j=0; j < nodeSize; j++) {
int randomValue = new Random().nextInt(maxValue);
myList.get(i).addValue(randomValue);
}
}
logMyList(myList, "myList unsorted"); // to see what we have
// now lets sort it
myList = sortListNode(myList);
logMyList(myList, "myList sorted"); // what we have after sorting
}
/**
* Selectionsort
*/
public ArrayList<ListNode> sortListNode(ArrayList<ListNode> myList) {
ArrayList<ListNode> retList = new ArrayList<ListNode>();
for ( int i = 0; i < listsize; i++) {
retList.add(new ListNode(nodeSize));
}
int lastSmallest = myList.get(0).getValueAt(0);
while ( !myList.isEmpty() ) {
int lastJ=0, lastI=0;
for ( int i = 0; i < myList.size(); i++) {
for (int j=0; j < myList.get(i).getSize(); j++) {
if ( myList.get(i).getValueAt(j) <= lastSmallest ) {
lastSmallest = myList.get(i).getValueAt(j);
lastJ = j;
lastI = i;
//log("Found smallest element at <"+i+","+j+"> (" + lastSmallest + ")");
}
}
}
myList.get(lastI).removeValue(lastJ);
if ( myList.get(lastI).getSize() == 0 )
myList.remove(lastI);
// add value to new list
for ( int i = 0; i < listsize; i++) {
if ( retList.get(i).getSize() < retList.get(i).getMaxSize() ) {
retList.get(i).addValue(lastSmallest);
break;
}
}
lastSmallest = Integer.MAX_VALUE;
}
return retList;
}
public class ListNode {
private ArrayList<Integer> values = new ArrayList<Integer>();
private int maxSize;
public ListNode(int maxSize) {
this.maxSize = maxSize;
}
public ArrayList<Integer> getValues() {
return values;
}
public int getMaxSize() {
return maxSize;
}
public int getSize() {
return values.size();
}
public int getValueAt(int position) {
if ( position < values.size())
return values.get(position);
else
throw new IndexOutOfBoundsException();
}
public void addValue(int value) {
values.add(value);
}
public void removeValue(int position) {
if ( position < values.size()) {
values.remove(position);
} else
throw new IndexOutOfBoundsException();
}
}
import java.util.ArrayList;
导入java.util.Random;
公共类ArrayedListSort{
int listsize=5;//有多少个节点
int maxValue=99;//最大值(此值为0)
int nodeSize=3;//每个节点的大小
公共静态void main(字符串[]args){
//非静态运行
新建ArrayedListSort().runTest();
}
/**
*日志函数。
*/
公共作废日志(对象s){
系统输出打印项次;
}
公共无效日志NOBR(对象s){
系统输出打印;
}
/**
*我们拥有的列表的输出。
*/
public void logMyList(ArrayList listNode,字符串名称){
日志(“==日志输出”+名称+“==”);
对于(int i=0;i if(myList.get(i).getValueAt(j)对不起,您在对包含在节点中的数组中的int进行排序(如从l
import java.util.ArrayList;
import java.util.Random;
public class ArrayedListSort {
int listsize = 5; // how many nodes
int maxValue = 99; // the highest value (0 to this)
int nodeSize = 3; // size for every node
public static void main(String[] args) {
// run non static
new ArrayedListSort().runTest();
}
/**
* Log function.
*/
public void log(Object s) {
System.out.println(s);
}
public void logNoBR(Object s) {
System.out.print(s);
}
/**
* Output of list we have.
*/
public void logMyList(ArrayList<ListNode> listNode, String name) {
log("=== LOG OUTPUT " + name + " ===");
for ( int i=0; i < listNode.size(); i++) {
logNoBR(" node <" + i + ">");
logNoBR(" (");
for (int j=0; j < listNode.get(i).getSize(); j++) {
if ( j != (listNode.get(i).getSize()-1)) // if not last add ","
logNoBR( listNode.get(i).getValueAt(j) + "," );
else
logNoBR( listNode.get(i).getValueAt(j) );
}
log(")");
}
log("=====================================\n");
}
public void runTest() {
// create example List
ArrayList<ListNode> myList = new ArrayList<ListNode>();
// fill the nodes with random values
for ( int i = 0; i < listsize; i++) {
myList.add(new ListNode(nodeSize));
for (int j=0; j < nodeSize; j++) {
int randomValue = new Random().nextInt(maxValue);
myList.get(i).addValue(randomValue);
}
}
logMyList(myList, "myList unsorted"); // to see what we have
// now lets sort it
myList = sortListNode(myList);
logMyList(myList, "myList sorted"); // what we have after sorting
}
/**
* Selectionsort
*/
public ArrayList<ListNode> sortListNode(ArrayList<ListNode> myList) {
ArrayList<ListNode> retList = new ArrayList<ListNode>();
for ( int i = 0; i < listsize; i++) {
retList.add(new ListNode(nodeSize));
}
int lastSmallest = myList.get(0).getValueAt(0);
while ( !myList.isEmpty() ) {
int lastJ=0, lastI=0;
for ( int i = 0; i < myList.size(); i++) {
for (int j=0; j < myList.get(i).getSize(); j++) {
if ( myList.get(i).getValueAt(j) <= lastSmallest ) {
lastSmallest = myList.get(i).getValueAt(j);
lastJ = j;
lastI = i;
//log("Found smallest element at <"+i+","+j+"> (" + lastSmallest + ")");
}
}
}
myList.get(lastI).removeValue(lastJ);
if ( myList.get(lastI).getSize() == 0 )
myList.remove(lastI);
// add value to new list
for ( int i = 0; i < listsize; i++) {
if ( retList.get(i).getSize() < retList.get(i).getMaxSize() ) {
retList.get(i).addValue(lastSmallest);
break;
}
}
lastSmallest = Integer.MAX_VALUE;
}
return retList;
}
public class ListNode {
private ArrayList<Integer> values = new ArrayList<Integer>();
private int maxSize;
public ListNode(int maxSize) {
this.maxSize = maxSize;
}
public ArrayList<Integer> getValues() {
return values;
}
public int getMaxSize() {
return maxSize;
}
public int getSize() {
return values.size();
}
public int getValueAt(int position) {
if ( position < values.size())
return values.get(position);
else
throw new IndexOutOfBoundsException();
}
public void addValue(int value) {
values.add(value);
}
public void removeValue(int position) {
if ( position < values.size()) {
values.remove(position);
} else
throw new IndexOutOfBoundsException();
}
}