- array.sort排序
java中Arrays.sort使用了两种排序方法,快速排序和优化的合并排序。
快速排序主要是对哪些基本类型数据(int,short,long等)排序, 而合并排序用于对对象类型进行排序。
使用不同类型的排序算法主要是由于快速排序是不稳定的,而合并排序是稳定的。这里的稳定是指比较相等的数据在排序之后仍然按照排序之前的前后顺序排列。对于基本数据类型,稳定性没有意义,而对于对象类型,稳定性是比较重要的,因为对象相等的判断可能只是判断关键属性,最好保持相等对象的非关键属性的顺序与排序前一直;另外一个原因是由于合并排序相对而言比较次数比快速排序少,移动(对象引用的移动)次数比快速排序多,而对于对象来说,比较一般比移动耗时。
补充一点合并排序的时间复杂度是n*logn, 快速排序的平均时间复杂度也是n*logn,但是合并排序的需要额外的n个引用的空间
java数据比较
- 实现java.lang.Comparable的接口;实现compareTo 方法
- 接受Object为参数
sort排序
list排序
import java.util.*;
class Interval{
int start;
int end;
Interval(int a, int b){
start=a;
end=b;
}
}
class Point implements Comparable<Point>{
int value;
int type;
Point(int v,int t){
value=v;
type=t;
}
public int compareTo(Point p){
if(this.value==p.value){
return 0;
}else if(this.value >p.value){
return 1;
}else {
return -1;
}
}
}
public class MyDemo{
public int getOverlappingCount(Interval[] A){
int max = 0,count = 1;
if(A==null || A.length==0) return max;
Point[] points = new Point[A.length*2];
for(int i = 0;i < A.length;i++){
points[2*i] = new Point(A[i].start, 0);
points[2*i+1] = new Point(A[i].end, 1);
}
Arrays.sort(points);
for (int i = 0; i < points.length; i++) {
if (points[i].type==0) {
count++;
max = Math.max(max, count);
}else{
count--;
}
}
return max;
}
public static void main(String[] args){
Interval[] testInterval=new Interval[4];
testInterval[0]=new Interval(1,5);
testInterval[1]=new Interval(10,15);
testInterval[2]=new Interval(5,10);
testInterval[3]=new Interval(20,30);
MyDemo demo=new MyDemo();
int max = demo.getOverlappingCount(testInterval);
System.out.println(max);
}
}
c++
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <Windows.h>
using namespace std;
class Interval
{
public:
Interval( int iStart, int iEnd)
:m_iStart( iStart), m_iEnd(iEnd){}
int m_iStart;
int m_iEnd;
};
typedef vector<Interval> IntervalVec;
class PointComparable
{
public:
PointComparable( int iVal, int iType )
:m_iVal( iVal ), m_iType( iType ){}
bool operator < ( const PointComparable& pcPoint )
{
if ( this->m_iVal == pcPoint.m_iVal )
{
return this->m_iType < pcPoint.m_iType;
}
return this->m_iVal < pcPoint.m_iVal;
}
int m_iVal;
int m_iType;
};
int GetOverlappedIntervalMaxCount( const IntervalVec& intvVec )
{
vector<PointComparable> pcVec;
for ( IntervalVec::const_iterator it = intvVec.begin();
it != intvVec.end(); ++it )
{
pcVec.push_back( PointComparable( it->m_iStart, 0 ) );
pcVec.push_back( PointComparable( it->m_iEnd, 1 ) );
}
sort( pcVec.begin(), pcVec.end() );
int iMaxCount = 0;
int iCurCount = 0;
for ( vector<PointComparable>::iterator itTemp = pcVec.begin();
itTemp != pcVec.end(); ++itTemp )
{
cout << itTemp->m_iVal << " " << itTemp->m_iType << endl;
if ( itTemp->m_iType == 0 )
{
iCurCount++;
iMaxCount = __max( iCurCount, iMaxCount );
}
else
{
iCurCount--;
}
}
return iMaxCount;
}
int main()
{
IntervalVec intvVec;
intvVec.push_back( Interval(1,2) );
intvVec.push_back( Interval(2,3) );
intvVec.push_back( Interval(3,4) );
intvVec.push_back( Interval(4,5) );
cout << "最大重叠区间个数:" << GetOverlappedIntervalMaxCount( intvVec )