77范文网 - 专业文章范例文档资料分享平台

JAVA冒泡、插入、选择排序算法

来源:网络收集 时间:2021-09-24 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

经常在笔试(或面试)中出现的JAVA经典算法,本人特此整理,希望有用

import java.io.*;

public class Paixu {

// 冒泡排序法 public void Maopao(int a[]) { for (int i = 1; i < a.length; i++) { for (int j = 0; j < a.length - i; j++) { if (a[j] > a[j + 1]) { int temp = a[j + 1]; a[j + 1] = a[j]; a[j] = temp; } } } } // 插入排序法: public void Charu(int a[]) { for (int i = 1; i < a.length; i++) { for (int j = 0; j < i; j++) { if (a[j] > a[i]) { int temp = a[i]; for (int k = i; k > j; k--) { a[k] = a[k--]; } a[j] = temp; } } } } // 选择排序法: public void Xuanze(int a[]) { for (int i = 0; i < a.length; i++) { int position = i; for (int j = i + 1; j < a.length; j++) { System.out.println("\n" + "采用插入排序法:"); System.out.println("\n" + "采用冒泡排序法:");

经常在笔试(或面试)中出现的JAVA经典算法,本人特此整理,希望有用

} } } if (a[position] > a[j]) { int temp = a[position]; a[position] = a[j]; a[j] = temp; } System.out.println("\n" + "采用选择排序法:"); public void Print(int a[]) { System.out.println("从小到大排序结果为:"); for (int i = 0; i < a.length; i++) { System.out.print(a[i] + ","); } } public static void main(String[] args) { int a[] = new int[5]; Paixu px = new Paixu(); BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); System.out.println("请输入五个整数:"); for (int i = 0; i < a.length; i++) { try { String s = buf.readLine(); int j = Integer.parseInt(s); a[i] = j; } catch (Exception e) { System.out.println("出错了!必须输入整数,请重新输入!");

i--; } } System.out.println("您输入的整数依次为:"); for (int i = 0; i < a.length; i++) { System.out.print(a[i] + ","); }

经常在笔试(或面试)中出现的JAVA经典算法,本人特此整理,希望有用

System.out.println("\n" + "-------------"); px.Maopao(a); // 调用冒泡算法 px.Print(a); System.out.println("\n" + "-------------"); px.Charu(a); // 调用插入算法 px.Print(a); System.out.println("\n" + "-------------"); px.Xuanze(a); // 调用选择算法

px.Print(a);

}

}

Java实现二分查找

2008-11-19 21:38

今天阿朗被问到二分查找竟然一着急没写出来。faint =。= 回来复习下

import java.util.*;

public class BinarySearch {

public static void main(String[] args) {

ArrayList<Integer> a = new ArrayList<Integer>();

addIntegerInSequence(a,1,10);

print(a);

int pos = binarySearch(a,10);

if ( pos != -1 )

{

System.out.print("Element found: " + pos);

}

else

{

System.out.print("Element not found");

}

}

/**

* 二分查找法

* @param a

* @param value 待查找元素

经常在笔试(或面试)中出现的JAVA经典算法,本人特此整理,希望有用

* @return

*/

public static int binarySearch(ArrayList<Integer> a, int value)

{

int size = a.size();

int low = 0 , high = size - 1;

int mid;

while (low <= high)

{

mid = (low + high) / 2;

if ( a.get(mid) < value )

{

low = low + 1;

}

else if ( a.get(mid) > value )

{

high = high - 1;

}

else

{

return mid;

}

}

return -1;

}

/**

* 填充顺序元素到数组

* @param a

* @param begin 开始元素

* @param size 大小

*/

public static void addIntegerInSequence(ArrayList<Integer> a, int begin, int size) {

for (int i = begin; i < begin + size; i++)

{

a.add(i);

}

}

/**

* 打印数组

* @param a

*/

经常在笔试(或面试)中出现的JAVA经典算法,本人特此整理,希望有用

public static void print(ArrayList<Integer> a)

{

Iterator<Integer> i = a.iterator();

while (i.hasNext())

{

System.out.print(i.next() + " ");

}

System.out.println("");

}

}

/////

JAVA 库中的二分查找使用非递归方式实现,返回结果与前面写的有所不同:找不到时返回的是负数,但不一定是-1

private static int binarySearch0(int[] a, int fromIndex, int toIndex,

int key) {

int low = fromIndex;

int high = toIndex - 1;

while (low <= high) {

int mid = (low + high) >>> 1;

int midVal = a[mid];

if (midVal < key)

low = mid + 1;

else if (midVal > key)

high = mid - 1;

else

return mid; // key found

}

return -(low + 1); // key not found.

}

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说医药卫生JAVA冒泡、插入、选择排序算法在线全文阅读。

JAVA冒泡、插入、选择排序算法.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/yiyao/1252791.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: