Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 明天考试啦。。。help
ggloverv





发贴: 5
积分: 0
于 2008-07-09 22:34 user profilesend a private message to usersend email to gglovervsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
题目:输入10个数,按由小到大的顺序排序后,再输出?

好像要用 java.io.*; 的System.in.read();



RVstudio
作者 Re:明天考试啦。。。help [Re:ggloverv]
weiyidexuan





发贴: 19
积分: 1
于 2008-07-11 01:30 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

//package com.ricky.www;
import java.util.Scanner;

public class OrderTheArray {
public static void main(String[] args){
System.out.println("input ten number:");
Scanner input = new Scanner(System.in);
int[] array = new int[10];
for(int i = 0;i < 10; i ++){
array[i] = input.nextInt();
}
array = orderTheArray(array);
for(int i = 0 ; i < 10; i ++){
System.out.print(array[i] + " ");
}
}

public static int[] orderTheArray(int[] array){
for(int i = 9; i > 0 ; i --){
for(int j = 0 ; j < i ; j ++){
int temp = 0;
if(array[i] < array[j]){
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
return array;
}
}

Note this is called a "Bubble sorting" if I am right. There are some other algorithms common for this task, which I will provide as follows.


HenryShanley edited on 2008-07-11 20:27

作者 Re:明天考试啦。。。help [Re:ggloverv]
JiafanZhou



版主


发贴: 736
积分: 61
于 2008-07-11 20:27 user profilesend a private message to usersend email to JiafanZhousearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Insertion Sort Array.


/**
* This is the array related package.
*/
package array;

import exception.ArrayException;

/**
* Demonstrates the insertion sort array.
* @author Jiafan Zhou
*/
public class InsertionSortArray
{
private long[] array;
private int nElements;

/**
* Constructor of this Selection Sort Array application.
* @param max the max items in this array.
*/
public InsertionSortArray(int max)
{
array = new long[max];
nElements = 0;
}

/**
* Insert the value into the array.
* @param value the inserted value
* @exception ArrayException when array is full in size
*/
public void insert(long value) throws ArrayException
{
if (dataSize() == arraySize())
{
throw new ArrayException("This array is full in size.");
}
array[nElements++] = value;
}

/**
* This is a linear search.
* Try to find the specified value in this array.
* @param searchKey search value
* @return true if found, false otherwise
*/
public boolean find(long searchKey)
{
for (int i = 0; i < nElements; i++)
{
if (array[i] == searchKey)
{
return true;
}
}
return false;
}

/**
* Try to delete the specified value in this array.
* If the value is deleted successfully, the old value
* will be replaced as a 0.
* @param value the value to be deleted
* @return true if deleted sucessfully, false otherwise.
*/
public boolean delete(long value)
{
for (int i = 0; i < nElements; i++)
{
if (array[i] == value)
{
for (int j = i; j < nElements - 1; j++)
{
array[j] = array[j + 1];
}
array[nElements - 1] = 0;
nElements--;
return true;
}
}
return false;
}

/**
* Return the size of this array.
* @return the size of this fixed array.
*/
public int arraySize()
{
return array.length;
}

/**
* Return the elements this array contained.
* @return the elements of this array contained.
*/
public int dataSize()
{
return nElements;
}

/**
* Clear all the elements in this array.
*/
public void clear()
{
for (int i = 0; i < array.length; i++)
{
array[i] = 0;
}
}

/**
* Displays array contents.
* @return displays array contents
*/
@Override
public String toString()
{
String string = "";
for (long i : array)
{
string += (i + " ");
}
return string;
}

/**
* This is an insertion sort(ascending).
*/
public void ascendingSort()
{
// marker is the *array index* where left is all sorted
// and right is all non-sorted elements.
for (int marker = 1; marker < nElements; marker++)
{
long temp = array[marker];
// insert the marker into the left sorted elements
int insert;
for (insert = marker; insert > 0; insert--)
{
if (array[insert - 1] > temp)
{
array[insert] = array[insert - 1];
}
else
{
break;
}
}
array[insert] = temp;
}
}

/**
* This is an insertion sort(descending).
*/
public void descendingSort()
{
// marker is the *array index* where left is all sorted
// and right is all non-sorted elements.
for (int marker = 1; marker < nElements; marker++)
{
long temp = array[marker];
// insert the marker into the left sorted elements
int insert;
for (insert = marker; insert > 0; insert--)
{
if (array[insert - 1] < temp)
{
array[insert] = array[insert - 1];
}
else
{
break;
}
}
array[insert] = temp;
}
}

}



When I was a kid I used to pray every night for a new bike. Then I realized that The Lord doesn't work that way, so I stole one and asked him to forgive me.
作者 Re:明天考试啦。。。help [Re:ggloverv]
JiafanZhou



版主


发贴: 736
积分: 61
于 2008-07-11 20:28 user profilesend a private message to usersend email to JiafanZhousearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Ordered Array (i.e. in this data structure, everything is in order)

/**
* This is the array related package.
*/
package array;

import exception.ArrayException;

/**
* Demonstrates ordered array class.
* @author Jiafan Zhou
*/
public class OrderedArray
{
private long[] array;
private int nElements;

/**
* Constructor of this Ordered Array application.
* @param max the max items in this array.
*/
public OrderedArray(int max)
{
array = new long[max];
nElements = 0;
}

/**
* Insert the value into the ordered array.
* This is a linear insert. (from smallest to biggest)
* @param value the inserted value.
* @exception ArrayException if the array is full.
*/
public void insert(long value) throws ArrayException
{
if (dataSize() == arraySize())
{
throw new ArrayException("This array is full in size.");
}

int index = 0;

// find where the new element goes
for (int i = 0; i < nElements; i++)
{
if (array[i] > value)
{
index = i;
break;
}
else
{
index = nElements;
}
}

//move bigger ones up
for (int i = nElements; i > index; i--)
{
array[i] = array[i - 1];
}

//insert the new value
array[index] = value;
nElements++;
}

/**
* This is a binary search. (needs to be an ordered array)
* Try to find the specified value in this array.
* @param searchKey search value
* @return true if found, false otherwise
*/
public boolean find(long searchKey)
{
int lowerBound = 0;
int upperBound = nElements - 1;

int currentIn;

while (true)
{
currentIn = (lowerBound + upperBound) / 2;

// found the searchKey
if (array[currentIn] == searchKey)
{
return true;
}
// can't find it
else if (lowerBound > upperBound)
{
return false;
}
else
{
// it is in upper half
if (array[currentIn] < searchKey)
{
lowerBound = currentIn + 1;
}
else //it is in lower half
{
upperBound = currentIn - 1;
}
}
}
}

/**
* This is a binary search. (needs to be an ordered array)
* Try to find the specified value in this array.
* @param searchKey search value
* @return if found, return the index of the specified value.
* if not found, return -1
*/
private int findIndex(long searchKey)
{
int lowerBound = 0;
int upperBound = nElements - 1;

int currentIn;

while (true)
{
currentIn = (lowerBound + upperBound) / 2;

// found the searchKey
if (array[currentIn] == searchKey)
{
return currentIn;
}
// can't find it
else if (lowerBound > upperBound)
{
return -1;
}
else
{
// it is in upper half
if (array[currentIn] < searchKey)
{
lowerBound = currentIn + 1;
}
else //it is in lower half
{
upperBound = currentIn - 1;
}
}
}
}

/**
* Try to delete the specified value in this array.
* If the value is deleted successfully, the old value
* will be replaced as a 0.
* This delete uses a binary search to find the value first.
* @param value the value to be deleted
* @return true if deleted sucessfully, false otherwise.
*/
public boolean delete(long value)
{
int index = findIndex(value);

if (index == -1)
{
return false;
}
else
{
//move bigger ones down
for (int j = index; j < nElements - 1; j++)
{
array[j] = array[j + 1];
}
array[nElements - 1] = 0;
nElements--;
return true;
}
}


/**
* Return the size of this array.
* @return the size of this fixed array.
*/
public int arraySize()
{
return array.length;
}

/**
* Return the elements this array contained.
* @return the elements of this array contained.
*/
public int dataSize()
{
return nElements;
}

/**
* Clear all the elements in this array.
*/
public void clear()
{
for (int i = 0; i < array.length; i++)
{
array[i] = 0;
}
}

/**
* Displays array contents.
* @return displays array contents
*/
@Override
public String toString()
{
String string = "";
for (long i : array)
{
string += (i + " ");
}
return string;
}

}



When I was a kid I used to pray every night for a new bike. Then I realized that The Lord doesn't work that way, so I stole one and asked him to forgive me.
作者 Re:明天考试啦。。。help [Re:ggloverv]
JiafanZhou



版主


发贴: 736
积分: 61
于 2008-07-11 20:29 user profilesend a private message to usersend email to JiafanZhousearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
The last one is called the "selection sorting", it is probably the most efficient one.

/**
* This is the array related package.
*/
package array;

import exception.ArrayException;

/**
* Demonstrates the selection sort array.
* @author Jiafan Zhou
*/
public class SelectionSortArray
{
private long[] array;
private int nElements;

/**
* Constructor of this Selection Sort Array application.
* @param max the max items in this array.
*/
public SelectionSortArray(int max)
{
array = new long[max];
nElements = 0;
}

/**
* Insert the value into the array.
* @param value the inserted value
* @exception ArrayException when array is full in size
*/
public void insert(long value) throws ArrayException
{
if (dataSize() == arraySize())
{
throw new ArrayException("This array is full in size.");
}
array[nElements++] = value;
}

/**
* This is a linear search.
* Try to find the specified value in this array.
* @param searchKey search value
* @return true if found, false otherwise
*/
public boolean find(long searchKey)
{
for (int i = 0; i < nElements; i++)
{
if (array[i] == searchKey)
{
return true;
}
}
return false;
}

/**
* Try to delete the specified value in this array.
* If the value is deleted successfully, the old value
* will be replaced as a 0.
* @param value the value to be deleted
* @return true if deleted sucessfully, false otherwise.
*/
public boolean delete(long value)
{
for (int i = 0; i < nElements; i++)
{
if (array[i] == value)
{
for (int j = i; j < nElements - 1; j++)
{
array[j] = array[j + 1];
}
array[nElements - 1] = 0;
nElements--;
return true;
}
}
return false;
}

/**
* Return the size of this array.
* @return the size of this fixed array.
*/
public int arraySize()
{
return array.length;
}

/**
* Return the elements this array contained.
* @return the elements of this array contained.
*/
public int dataSize()
{
return nElements;
}

/**
* Clear all the elements in this array.
*/
public void clear()
{
for (int i = 0; i < array.length; i++)
{
array[i] = 0;
}
}

/**
* Displays array contents.
* @return displays array contents
*/
@Override
public String toString()
{
String string = "";
for (long i : array)
{
string += (i + " ");
}
return string;
}

/**
* This is a selection sort(ascending).
*/
public void ascendingSort()
{
int minIndex;

for (int i = 0; i < nElements - 1; i++)
{
minIndex = i;

for (int j = i + 1; j < nElements; j++)
{
if (array[j] < array[minIndex])
{
minIndex = j;
}
}
long temp = array[i];
array[i] = array[minIndex];
array[minIndex] = temp;
}
}

/**
* This is a selection sort(descending).
*/
public void descendingSort()
{
int maxIndex;

for (int i = 0; i < nElements - 1; i++)
{
maxIndex = i;

for (int j = i + 1; j < nElements; j++)
{
if (array[j] > array[maxIndex])
{
maxIndex = j;
}
}
long temp = array[i];
array[i] = array[maxIndex];
array[maxIndex] = temp;
}
}
}



When I was a kid I used to pray every night for a new bike. Then I realized that The Lord doesn't work that way, so I stole one and asked him to forgive me.
作者 Re:明天考试啦。。。help [Re:ggloverv]
Miragi_Song





发贴: 2
积分: 0
于 2008-07-12 02:38 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
I always use bubble sort to make an array ascending or descending(arithmetically or alphabetically)..make my life easier/_\


作者 Re:明天考试啦。。。help [Re:ggloverv]
ggloverv





发贴: 5
积分: 0
于 2008-07-12 15:18 user profilesend a private message to usersend email to gglovervsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Don't you think that is too long?

import java.util.Scanner;
import java.util.TreeSet;

class Test2{
  public static void main(String args[]){
    Scanner scan = new Scanner(System.in);
    TreeSet set = new TreeSet();
    
    for (int i=1;i<=10;i++){
      System.out.println("输入第"+i+"个数:");
      int d = scan.nextInt();
      set.add( d );
    }
    
    for(Object o:set){
      System.out.print(o.toString());
    }
  }
}



RVstudio
作者 Re:明天考试啦。。。help [Re:ggloverv]
wusky





发贴: 4
积分: 0
于 2008-07-14 02:05 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list



作者 Re:明天考试啦。。。help [Re:Miragi_Song]
JiafanZhou



版主


发贴: 736
积分: 61
于 2008-07-15 16:19 user profilesend a private message to usersend email to JiafanZhousearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Miragi_Song wrote:
I always use bubble sort to make an array ascending or descending(arithmetically or alphabetically)..make my life easier/_\

Bubble sort is not a very good algorithm in a lot of circumstances. The worst scenario is you want to sort an array which is in totally reverse order. Then the life will not be that easy for computers. Smile

Jiafan



When I was a kid I used to pray every night for a new bike. Then I realized that The Lord doesn't work that way, so I stole one and asked him to forgive me.

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923