bubble:
public class bubbleSort {
private int nNum;
private int[] str;
public bubbleSort(int i){
str = new int[i];
}
public void insert(int i){
str[nNum] = i;
nNum++;
}
public boolean del(int i){
for(int root = 0;root<str.length;root++){
if(str[root]==i&&str[root+1]!=0){
for(int del = root;del<str.length;del++){
str[root] = str[root+1];
return true;
}
}
else if(str[root]==i&&str[root+1]==0){
str[root]=0;
return true;
}
}
return false;
}
public void print(){
for(int g = 0;g<str.length;g++){
System.out.print(str[g]);
System.out.print(" ");
}
}
public void sort(){
int flag;
int out = str.length;
for(int j = out-1;j>=0;j--){
for(int i = 0;i<j;i++){
if (str[i]>str[i+1]){
flag = str[i];
str[i] = str[i+1];
str[i+1] = flag;
}
}
}
System.out.println("");
System.out.println("after bublle sort::");
}
}
class bubbleSortApp{
public bubbleSortApp(){
bubbleSort num = new bubbleSort(10);
num.insert(45);
num.insert(56);
num.insert(78);
num.insert(44);
num.insert(79);
num.insert(34);
num.insert(98);
num.insert(678);
num.insert(787);
num.insert(789);
num.print();
num.sort();
num.print();
}
public static void main(String[] args){
new bubbleSortApp();
}
}
select:
public class selectSort {
private int nNum;
private int[] str;
public selectSort(int i){
str = new int[i];
}
public void insert(int i){
str[nNum] = i;
nNum++;
}
public boolean del(int i){
for(int root = 0;root<str.length;root++){
if(str[root]==i&&str[root+1]!=0){
for(int del = root;del<str.length;del++){
str[root] = str[root+1];
return true;
}
}
else if(str[root]==i&&str[root+1]==0){
str[root]=0;
return true;
}
}
return false;
}
public void print(){
for(int g = 0;g<str.length;g++){
System.out.print(str[g]);
System.out.print(" ");
}
}
public void sort(){
int flag = str[0];
int head = str.length;
for(int i = 0;i<head;i++){
for(int j = i;j<head;j++){
if(str[j]<flag) flag = str[j];
}
int temp;
temp = flag;
flag = str[i];
str[i] = temp;
}
System.out.println("");
System.out.println("after select sort::");
}
}
class selectSortApp{
public selectSortApp(){
selectSort num = new selectSort(10);
num.insert(45);
num.insert(56);
num.insert(78);
num.insert(44);
num.insert(79);
num.insert(34);
num.insert(98);
num.insert(678);
num.insert(787);
num.insert(789);
num.print();
num.sort();
num.print();
}
public static void main(String[] args){
new selectSortApp();
}
}
insert:
public class insertSort {
private int nNum;
private int[] str;
public insertSort(int i){
str = new int[i];
}
public void insert(int i){
str[nNum] = i;
nNum++;
}
public boolean del(int i){
for(int root = 0;root<str.length;root++){
if(str[root]==i&&str[root+1]!=0){
for(int del = root;del<str.length;del++){
str[root] = str[root+1];
return true;
}
}
else if(str[root]==i&&str[root+1]==0){
str[root]=0;
return true;
}
}
return false;
}
public void print(){
for(int g = 0;g<str.length;g++){
System.out.print(str[g]);
System.out.print(" ");
}
}
public void sort(){
for(int a = 1;a<nNum;a++){
int right = str[a];
int left = a;
while(left>0&&str[left-1]>=right){
str[left] = str[left-1];
--left;
}
str[left] = right;
}
System.out.println("");
System.out.println("after insert sort::");
}
}
class insertSortApp{
public insertSortApp(){
insertSort num = new insertSort(10);
num.insert(45);
num.insert(16);
num.insert(78);
num.insert(44);
num.insert(1);
num.insert(34);
num.insert(98);
num.insert(678);
num.insert(787);
num.insert(389);
num.print();
num.sort();
num.print();
}
public static void main(String[] args){
new insertSortApp();
}
}