java冒泡排序和快速排序代码

网友投稿 293 2023-05-25


java冒泡排序和快速排序代码

冒泡排序:

基本思想:在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒。即:每当两相邻的数比较后发现它们的排序与排序要求相反时,就将它们互换。

public class BubbleSorted{

public BubbleSorted(){

int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};

int temp=0;

for(int i=0;i

for(int j=0;j

if(a[j]>a[j+1]){

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

}

}

}

for(int i=0;i

System.out.println(a[i]);

} }

快速排序:

算法:当数据量很大适宜采用该方法。采用二分法查找时,数据需是有序不重复的。 基本思想:假设数据是按升序排序的,对于给定值 x,从序列的中间位置开始比较,如果当前位置值等于 x,则查找成功;若 x 小于当前位置值,则在数列的前半段中查找;若 x 大于当前位置值则在数列的后半段中继续查找,直到找到为止。

假设有一个数组 { 12, 23, 34, 45, 56, 67, 77, 89, 90 },现要求采用二分法找出指定的数值并将其在数组的索引返回,如果没有找到则返回 -1。代码如下:

package com.test;

public class FindSorted{

public static void main(String[] args) {

int[] arr = new int[] { 12, 23, 34, 45, 56, 67, 77, 89, 90 };

System.out.println(search(arr, 12));

System.out.println(search(arr, 45));

System.out.println(search(arr, 67));

System.out.println(search(arr, 89));

System.out.println(search(arr, 99));

}

public static int search(int[] arr, int key) {

int start = 0;

int end = arr.length - 1;

while (start <= end) {

int middle = (start + end) / 2;

if (key < arr[middle]) {

end = middle - 1http://;

} else if (key > arr[middle]) {

start = middle + 1;

} else {

return middle;

}

}

return -1;

}

}

for(int j=0;j

if(a[j]>a[j+1]){

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

}

}

}

for(int i=0;i

System.out.println(a[i]);

} }

快速排序:

算法:当数据量很大适宜采用该方法。采用二分法查找时,数据需是有序不重复的。 基本思想:假设数据是按升序排序的,对于给定值 x,从序列的中间位置开始比较,如果当前位置值等于 x,则查找成功;若 x 小于当前位置值,则在数列的前半段中查找;若 x 大于当前位置值则在数列的后半段中继续查找,直到找到为止。

假设有一个数组 { 12, 23, 34, 45, 56, 67, 77, 89, 90 },现要求采用二分法找出指定的数值并将其在数组的索引返回,如果没有找到则返回 -1。代码如下:

package com.test;

public class FindSorted{

public static void main(String[] args) {

int[] arr = new int[] { 12, 23, 34, 45, 56, 67, 77, 89, 90 };

System.out.println(search(arr, 12));

System.out.println(search(arr, 45));

System.out.println(search(arr, 67));

System.out.println(search(arr, 89));

System.out.println(search(arr, 99));

}

public static int search(int[] arr, int key) {

int start = 0;

int end = arr.length - 1;

while (start <= end) {

int middle = (start + end) / 2;

if (key < arr[middle]) {

end = middle - 1http://;

} else if (key > arr[middle]) {

start = middle + 1;

} else {

return middle;

}

}

return -1;

}

}

if(a[j]>a[j+1]){

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

}

}

}

for(int i=0;i

System.out.println(a[i]);

} }

快速排序:

算法:当数据量很大适宜采用该方法。采用二分法查找时,数据需是有序不重复的。 基本思想:假设数据是按升序排序的,对于给定值 x,从序列的中间位置开始比较,如果当前位置值等于 x,则查找成功;若 x 小于当前位置值,则在数列的前半段中查找;若 x 大于当前位置值则在数列的后半段中继续查找,直到找到为止。

假设有一个数组 { 12, 23, 34, 45, 56, 67, 77, 89, 90 },现要求采用二分法找出指定的数值并将其在数组的索引返回,如果没有找到则返回 -1。代码如下:

package com.test;

public class FindSorted{

public static void main(String[] args) {

int[] arr = new int[] { 12, 23, 34, 45, 56, 67, 77, 89, 90 };

System.out.println(search(arr, 12));

System.out.println(search(arr, 45));

System.out.println(search(arr, 67));

System.out.println(search(arr, 89));

System.out.println(search(arr, 99));

}

public static int search(int[] arr, int key) {

int start = 0;

int end = arr.length - 1;

while (start <= end) {

int middle = (start + end) / 2;

if (key < arr[middle]) {

end = middle - 1http://;

} else if (key > arr[middle]) {

start = middle + 1;

} else {

return middle;

}

}

return -1;

}

}

System.out.println(a[i]);

} }

快速排序:

算法:当数据量很大适宜采用该方法。采用二分法查找时,数据需是有序不重复的。 基本思想:假设数据是按升序排序的,对于给定值 x,从序列的中间位置开始比较,如果当前位置值等于 x,则查找成功;若 x 小于当前位置值,则在数列的前半段中查找;若 x 大于当前位置值则在数列的后半段中继续查找,直到找到为止。

假设有一个数组 { 12, 23, 34, 45, 56, 67, 77, 89, 90 },现要求采用二分法找出指定的数值并将其在数组的索引返回,如果没有找到则返回 -1。代码如下:

package com.test;

public class FindSorted{

public static void main(String[] args) {

int[] arr = new int[] { 12, 23, 34, 45, 56, 67, 77, 89, 90 };

System.out.println(search(arr, 12));

System.out.println(search(arr, 45));

System.out.println(search(arr, 67));

System.out.println(search(arr, 89));

System.out.println(search(arr, 99));

}

public static int search(int[] arr, int key) {

int start = 0;

int end = arr.length - 1;

while (start <= end) {

int middle = (start + end) / 2;

if (key < arr[middle]) {

end = middle - 1http://;

} else if (key > arr[middle]) {

start = middle + 1;

} else {

return middle;

}

}

return -1;

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信小程序 页面跳转如何实现传值
下一篇:Eclipse项目有红感叹号的解决方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~