参考博客http://blog.chinaunix.net/uid-28311809-id-4267159.html,谢谢博主!
同时求最大值最小值,这个算法会少一些比较次数:
python版的代码:
# -*- coding: UTF-8 -*-
def find_max_min(A):
print "寻找最大最小值"
max_num = min_num = 0
j = 0
if len(A)%2==0:
if A[j]<A[j+1]:
min_num = A[j];j=j+1
max_num = A[j];j=j+1
else:
max_num = A[j];j=j+1
min_num = A[j];j=j+1
else:
max_num = min_num = A[j];j=j+1
for i in range(j,len(A)-1,2):
if A[i]>A[i+1]:
max_num = max(A[i] , max_num)
min_num = min(A[i+1],min_num)
else:
max_num = max(A[i+1] , max_num)
min_num = min(A[i],min_num)
return max_num,min_num
print find_max_min((1,22,3))
</pre><pre name="code" class="python">
不得不说python这种语言很是方便,当然除了++这个操作之外
附上java版本的代码:
package com.pingan.mhc.bishiti;
/**
* 同时寻找最大值和最小值,先比较一次数组中取出来的两个值,再分别与最大值最小值比较,会减少一次比较的运算
*
*/
public class find_max_min {
public static int max,min;//默认为0
public static void findMM(int[] array){
int len = array.length;
int j = 0;
if(len %2 == 0){
if(array[j]>array[j+1]){
max = array[j++];
min = array[j++];
}else{
min = array[j++];
max = array[j++];
}
}else{
max = min = array[j++];
}
for(int i = j;j<len;j+=2){
if(array[i]>array[i+1]){
max = Math.max(max, array[i] );
min = Math.min(min, array[i+1]);
}else{
max = Math.max(max,array[i+1]);
min = Math.min(min, array[i]);
}
}
}
public static void main(String[] args) {
int[] array = new int[]{1,4,7,10,3,2};
findMM(array);
System.out.println("最大值是:"+max);
System.out.println("最小值是:"+min);
}
}
再说第K大的值:
先来一个算法导论中,输入一个随机的数,然后将比该值大的和小的分开的函数。快速排序中的partion算法。
package com.pingan.mhc.bishiti;
public class partion {
public static int [] array = new int [] {3,2,14,1,13,24};
//交换函数
public static void swap(int a,int b){
int temp = array[a];
array[a] = array[b];
array[b] = temp;
}
//实现随机输入数字,通过该函数,该数字之前的数字都比该数字小,后面的都不小于该数字
public static void partion(int start,int end,int pivotIndex){
int tempIndex = start;
int tempData = array[pivotIndex];
// 将pivotIndex位置上的值换到最后边
swap(end,pivotIndex);
for(int i = start;i<end;i++){
if(tempData > array[i]){
swap(i,tempIndex++);
}
}
swap(tempIndex,end);
System.out.println(tempIndex);
}
public static void main(String[] args) {
partion(0,array.length-1,1);
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]+" ");
}
}
}
求第K大的数:
package com.pingan.mhc.bishiti;
//求第K大的数
import org.junit.Test;
public class MaxKth {
int k = 5;
int[] a = new int[]{4,2,11,5,13,29,101};
public void swap(int x,int y){
/* 这种交换如果出现自己跟自己交换的时候就会变为0
* a[x] ^= a[y];
a[y] ^= a[x];
a[x] ^= a[y];*/
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}
public void quickSort(int begin,int end){
if(begin < end){
int mid = partion(begin,end);
if(k>=1 && k<=mid-1){
quickSort(begin,mid-1);
}else{
quickSort(mid+1,end);
}
}
}
public int partion(int begin, int end) {
//以第一个作为分界
int key = a[begin],index = begin;
for (int i = begin+1; i < a.length; i++) {
if(a[i]<key){
swap(++index,i);
}
}
swap(begin,index);
return index;
}
@Test
public void test(){
quickSort(0, a.length-1);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
System.out.println("第"+(k+1)+"大的数:"+a[k]);
}
}