数组的快速排序 nlogn 空间复杂度为log(n)
public static int partion(int [] a,int from,int to){
if(a==null) return -1;
int base=a[from];
while(from<to){
while(from<to&&a[to]>base){
to--;
}
if(from<to){
a[from++]=a[to];
}
while(from<to&&a[from]<base){
from++;
}
if(from<to){
a[to--]=a[from];
}
}
a[from]=base;
return from;
}
public static void fastSort(int [] a,int low,int high){
if(low>high||a==null) return;
if(low<=high){
int index=partion(a,low,high);
fastSort(a,low,index-1);
fastSort(a,index+1,high);
}
}
链表的快速排序 nlogn 空间复杂度是logn
public ListNode sortList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode dummy1 = new ListNode(0);
ListNode dummy2 = new ListNode(0);
ListNode prev1 = dummy1;
ListNode prev2 = dummy2;
ListNode node = head.next;
ListNode prev = head;
while (node != null) {
if (node.val < head.val) {
prev1.next = node;
prev1 = prev1.next;
} else if (node.val > head.val) {
prev2.next = node;
prev2 = prev2.next;
} else {
prev.next = node;
prev = prev.next;
}
node = node.next;
}
prev1.next = prev2.next = prev.next = null;
dummy1.next = sortList(dummy1.next);
dummy2.next = sortList(dummy2.next);
prev1 = dummy1;
while (prev1.next != null) {
prev1 = prev1.next;
}
prev1.next = head;
prev.next = dummy2.next;
return dummy1.next;
}
归并排序 nlogn 空间是o(n)
public static void merge(int[] a, int from, int mid, int to) {
if (from >= to)
return;
int[] save = new int[to - from + 1];
int i = from, j = mid + 1, k = 0;
while (i <= mid && j <= to) {
if (a[i] <= a[j]) {
save[k++] = a[i++];
} else
save[k++] = a[j++];
}
while (i <= mid) {
save[k++] = a[i++];
}
while (j <= to) {
save[k++] = a[j++];
}
for (int k2 = 0; k2 < save.length; k2++) {
a[from + k2] = save[k2];
}
}
public static void sort(int[] a, int from, int to) {
if (from >= to || a == null)
return;
int mid = from + (to - from) / 2;
if (from < to) {
sort(a, from, mid);
sort(a, mid + 1, to);
merge(a, from, mid, to);
}
}
链表的归并排序
public ListNode sortList(ListNode head) {
if(head==null||head.next==null) return head;
ListNode fast=fast.next.next,slow=head;
while(fast!=null&&fast.next!=null){
fast=fast.next.next;
slow=slow.next;
}
fast=slow.next;
slow.next=null;
ListNode p1=sortList(fast);
ListNode p2=sortList(head);
return merge(p1,p2);
}
public ListNode merge(ListNode l1,ListNode l2){
if(l1==null) return l2;
if(l2==null) return l1;
ListNode dump=new ListNode(0);
ListNode p=dump;
while(l1!=null&&l2!=null){
if(l1.val<l2.val){
p.next=l1;
p=p.next;
l1=l1.next;
}
else{
p.next=l2;
l2=l2.next;
p=p.next;
}
}
while(l1!=null){
p.next=l1;
p=p.next;
l1=l1.next;
}
while(l2!=null){
p.next=l2;
l2=l2.next;
p=p.next;
}
return dump.next;
}
数组的插入排序 O(n2)
public static void insertSort(int [] a){
if(a.length<=1) return;
for(int i=1;i<a.length;i++){
int tmp=a[i];
int j=i-1;
while(j>=0&&a[j]>tmp){
a[j+1]=a[j];
j--;
}
a[j+1]=tmp;
}
}
链表的插入排序
从左向右找到第一个大于当前值的位置,放到其前面,根据链表的特点,要比较.next.val,这样才能放到其前面
改进点是不一定每次都需要从头开始找,只有点高当前值比排好的最后一位小的时候才需要
public static ListNode insertSort(ListNode head){
if(head==null||head.next==null) return head;
ListNode dump=new ListNode(0);
ListNode p=dump,cur=head;//p是辅助,cur是当前开始排哪个了
while(cur!=null){
if(p!=dump&&p.val>cur.val){
p=dump;
}
while(p.next!=null&&cur.val>=p.next.val){
p=p.next;
}
ListNode tmp=cur.next;
cur.next=p.next;
p.next=cur;
cur=tmp;//移位
}
return dump.next;
}
排序
最新推荐文章于 2025-01-08 17:00:00 发布