Topic: 明天考试啦。。。help

  Print this page

1.明天考试啦。。。help Copy to clipboard
Posted by: ggloverv
Posted on: 2008-07-09 22:34

题目:输入10个数,按由小到大的顺序排序后,再输出?

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

2.Re:明天考试啦。。。help [Re: ggloverv] Copy to clipboard
Posted by: weiyidexuan
Posted on: 2008-07-11 01:30


//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.

3.Re:明天考试啦。。。help [Re: ggloverv] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2008-07-11 20:27

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;
}
}

}

4.Re:明天考试啦。。。help [Re: ggloverv] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2008-07-11 20:28

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;
}

}

5.Re:明天考试啦。。。help [Re: ggloverv] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2008-07-11 20:29

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;
}
}
}

6.Re:明天考试啦。。。help [Re: ggloverv] Copy to clipboard
Posted by: Miragi_Song
Posted on: 2008-07-12 02:38

I always use bubble sort to make an array ascending or descending(arithmetically or alphabetically)..make my life easier/_\

7.Re:明天考试啦。。。help [Re: ggloverv] Copy to clipboard
Posted by: ggloverv
Posted on: 2008-07-12 15:18

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());
    }
  }
}

8.Re:明天考试啦。。。help [Re: ggloverv] Copy to clipboard
Posted by: wusky
Posted on: 2008-07-14 02:05


9.Re:明天考试啦。。。help [Re: Miragi_Song] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2008-07-15 16:19

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


   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