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

您没有登录

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

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 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.

话题树型展开
人气 标题 作者 字数 发贴时间
12532 明天考试啦。。。help ggloverv 65 2008-07-09 22:34
9521 Re:明天考试啦。。。help weiyidexuan 936 2008-07-11 01:30
9112 Re:明天考试啦。。。help JiafanZhou 4637 2008-07-11 20:27
9084 Re:明天考试啦。。。help JiafanZhou 5697 2008-07-11 20:28
9199 Re:明天考试啦。。。help JiafanZhou 4261 2008-07-11 20:29
9258 Re:明天考试啦。。。help Miragi_Song 123 2008-07-12 02:38
9487 Re:明天考试啦。。。help JiafanZhou 383 2008-07-15 16:19
9473 Re:明天考试啦。。。help ggloverv 762 2008-07-12 15:18
9403 Re:明天考试啦。。。help wusky 0 2008-07-14 02:05

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