Leetcode 912. 排序数组(DAY 195)


原题题目


在这里插入图片描述


代码实现(首刷三分钟 手撕快排递归)


class Solution {
public:
    int partition(vector<int>& nums,int left,int right)
    {
        int lo = left,hi = right + 1,mid = (lo + (hi - lo) / 2);
        swap(nums[left],nums[mid]);
        while(lo < hi)
        {
            while(lo < right && nums[++lo] <= nums[left]);
            while(hi > left && nums[--hi] >= nums[left]);
            if(lo < hi)
                swap(nums[lo],nums[hi]);
        }

        swap(nums[hi],nums[left]);
        return hi;
    }

    void qsort(vector<int>& nums,int left,int right)
    {
        if(left >= right)   return;
        int mid = partition(nums,left,right);
        qsort(nums,left,mid - 1);
        qsort(nums,mid + 1,right);
    }

    vector<int> sortArray(vector<int>& nums) {
        qsort(nums,0,nums.size() - 1);
        return nums;
    }
};

代码实现(首刷自解 快排非递归 TLE 8/13)


class Solution {
public:
    int partition(vector<int>& nums,int left,int right)
    {
        int lo = left,hi = right + 1,mid = (lo + (hi - lo) / 2);
        swap(nums[left],nums[mid]);
        while(lo < hi)
        {
            while(lo < right && nums[++lo] <= nums[left]);
            while(hi > left && nums[--hi] >= nums[left]);
            if(lo < hi)
                swap(nums[lo],nums[hi]);
        }

        swap(nums[hi],nums[left]);
        return hi;
    }

    void qsort(vector<int>& nums,int left,int right)
    {
        if(left >= right)   return;
        stack<pair<int,int>> stack;
        stack.emplace(left,right);
        while(!stack.empty())
        {
            auto pair = stack.top();
            stack.pop();
            int lo = pair.first,hi = pair.second;
            int mid = partition(nums,lo,hi);
            if(mid > lo)  stack.emplace(left,mid - 1);
            if(mid < hi)  stack.emplace(mid + 1,hi);
        }
    }

    vector<int> sortArray(vector<int>& nums) {
        qsort(nums,0,nums.size() - 1);
        return nums;
    }
};

代码实现(二刷自解 堆排序 DAY 198 C++)


class Solution {
public:
    class minheap
    {
    public:
        vector<int> heap;
        minheap() {heap.clear();}
        minheap(vector<int>& v)
        { 
            heap = v;
            for(int i = (heap.size() - 1) / 2;i >= 0;--i)
                adjustheap(heap,i);
        }

        void adjustheap(vector<int>& heap,int pos)
        {
            if(heap.empty() || pos == heap.size()-1)
                return;

            int val = heap[pos];
            int maxpos = heap.size() - 1;
            for(int i = pos*2+1;i <= maxpos;i = pos*2+1)
            {
                if(i < maxpos && heap[i+1] < heap[i])   ++i;
                if(val <= heap[i])  break;

                heap[pos] = heap[i];
                pos = i;
            }
            heap[pos] = val;
        }

        void push(int val)
        {
            int pos = heap.size();
            heap.emplace_back(val);
            for(;val < heap[(pos - 1)/2];pos = (pos - 1)/2)
            {
                heap[pos] = heap[(pos - 1) / 2];
                if(pos == 0)    break;
            }
            heap[pos] = val;
        }

        int pop()
        {
            int ret = heap[0];
            int pos = heap.size() - 1;
            heap[0] = heap.back();
            heap.pop_back();
            adjustheap(heap,0);
            return ret;
        }

        bool empty()
        {
            return !heap.size();
        }

    };

    vector<int> sortArray(vector<int>& nums) {
        minheap heap(nums);
        vector<int> ret;

        while(!heap.empty())
            ret.emplace_back(heap.pop());
        return ret;
    }
};

代码实现(三刷自解 归并排序 DAY 213 C++)


class Solution {
public:
    void merge_nums(vector<int>& nums,vector<int>& tmp,int left,int right)
    {
        int l = left,mid = (left + right) / 2,r = mid+1,pos = left;
        while(l <= mid || r <= right)
        {
            if(l > mid || r > right)
            {
                if(l > mid)   tmp[pos++] = nums[r++];
                else          tmp[pos++] = nums[l++];
            }
            else
            {
                if(nums[l] <= nums[r])
                    tmp[pos++] = nums[l++];
                else
                    tmp[pos++] = nums[r++];
            }
        }

        for(int i=left;i<=right;++i)
            nums[i] = tmp[i];
    }

    void merge_sort(vector<int>& nums,vector<int>& tmp,int left,int right)
    {
        if(left >= right)   return; 
        int mid = (left + right) / 2;

        merge_sort(nums,tmp,left,mid);
        merge_sort(nums,tmp,mid+1,right);
        merge_nums(nums,tmp,left,right);
    }

    vector<int> sortArray(vector<int>& nums) {
        vector<int> tmp(nums.size(),0);
        merge_sort(nums,tmp,0,nums.size()-1);
        return nums;
    }
};

代码实现(四刷自解 DAY 269 C++)


class Solution {
public:
    void merge(vector<int>& nums, vector<int>& tmp, int left, int right)
    {
      int mid = ((right - left) >> 1) + left;
      int l = left,r = mid + 1,pos = left;
      while(l <= mid && r <= right)
      {
        if(nums[l] <= nums[r])
          tmp[pos++] = nums[l++];
        else
          tmp[pos++] = nums[r++];
      }

      while(l <= mid)   tmp[pos++] = nums[l++];
      while(r <= right) tmp[pos++] = nums[r++];

      for(int i = left; i <= right; ++i)
        nums[i] = tmp[i];
      
      return;
    } 

    void merge_sort(vector<int>& nums, vector<int>& tmp, int left, int right)
    {
      if( left >= right )   return;

      int mid = ((right - left) >> 1) + left;
      merge_sort(nums, tmp, left, mid);
      merge_sort(nums, tmp, mid + 1, right);
      merge(nums, tmp, left, right);
    }

    vector<int> sortArray(vector<int>& nums) {
        vector<int> tmp(nums);
        merge_sort(nums, tmp, 0, nums.size() - 1);
        return nums; 
    }
};

代码实现(五刷自解 堆排序 DAY 293 C++)


class Heap {
 public:
  Heap() {}
  Heap(vector<int>& heap__) : heap_(heap__) {
    for (int i = heap_.size() / 2 - 1; i >= 0; --i) {
      Heapify(i);
    }    
  }
  
  void Heapify(int pos) {
    int val = heap_[pos];
    int nextpos = pos * 2 + 1;

    for (; nextpos < heap_.size(); nextpos = nextpos * 2 + 1) {
      if (nextpos < heap_.size() - 1 && heap_[nextpos + 1] < heap_[nextpos]) {
        ++nextpos;
      }

      if (val > heap_[nextpos]) {
        heap_[pos] = heap_[nextpos]; 
        pos = nextpos;
      } else {
        break;
      }
    }
    heap_[pos] = val;
  }

  void Push(int val) {
    heap_.emplace_back(val);
    
    int pos = heap_.size() - 1;
    while (pos > 0) {
      int nextpos = (pos - 1) / 2; 
      if (heap_[nextpos] > val) {
        heap_[pos] = heap_[nextpos];
        pos = nextpos;
      } else {
        break;
      }
    }
    heap_[pos] = val;
  }

  void Pop() {
    int lastelem = heap_.back();
    heap_.pop_back();
    heap_[0] = lastelem;
    Heapify(0);
  }

  int Top() const {
    return heap_[0];
  }

  bool Empty() const {
    return heap_.size() == 0;
  }

 private:
  vector<int> heap_;
};

class Solution {
 public:
  vector<int> sortArray(vector<int>& nums) {
    vector<int> ret;
    Heap heap;

    for (const auto& num : nums) {
      heap.Push(num);
    }

    while (!heap.Empty()) {
      ret.emplace_back(heap.Top());
      heap.Pop();
    }
    return ret;
  }
};

代码实现(六刷自解 归并排序 DAY 300 C++)


class Solution {
 public:
  void Merge(vector<int>& nums, vector<int>& tmp, int left, int right) {
    int mid = (left + right) / 2;
    int l = left, r = mid + 1;
    int pos = l;
    while (l <= mid && r <= right) {
      if (nums[l] <= nums[r]) {
        tmp[pos++] = nums[l++];
      } else {
        tmp[pos++] = nums[r++];
      }
    }

    while (l <= mid) tmp[pos++] = nums[l++];
    while (r <= right) tmp[pos++] = nums[r++];

    for (int i = left; i <= right; ++i) {
      nums[i] = tmp[i];
    }
  }

  void MergeSort(vector<int>& nums, vector<int>& tmp, int left, int right) {
    if (left >= right)  return;
    int mid = (left + right) / 2;
    MergeSort(nums, tmp, left, mid);
    MergeSort(nums, tmp, mid + 1, right);
    Merge(nums, tmp, left, right);
  }

  vector<int> sortArray(vector<int>& nums) {
    vector<int> tmp(nums.size(), 0);
    MergeSort(nums, tmp, 0, nums.size() - 1);
    return nums;
  }
};

代码实现(七刷自解 DAY 1 Golang)


func swap(x, y int) (int, int) {
  return y, x
}

func QuickSort(nums []int, left, right int) {
  if (left >= right) {
    return
  }

  mid := ((right - left) >> 1) + left
  nums[left], nums[mid] = swap(nums[left], nums[mid])
  l, r, pivot := left, right + 1, nums[left]

  for l < r {
    for l < right {
      l++
      if nums[l] >= pivot {
        break
      }
    }

    for r > left {
      r--
      if nums[r] <= pivot {
        break
      }
    }
    if l < r {
      nums[l], nums[r] = swap(nums[l], nums[r])
    } else {
      break
    }
  }

  nums[left], nums[r] = swap(nums[left], nums[r])  
  QuickSort(nums, left, r - 1)
  QuickSort(nums, r + 1, right)
}

func sortArray(nums []int) []int {
  QuickSort(nums, 0, len(nums) - 1)
  return nums
}

代码实现(八刷自解 DAY 10 Golang)


type Heap struct {
  heap []int
}

func (this *Heap) Construct(nums []int) {
  this.heap = nums
  for i := len(this.heap) / 2; i >= 0; i-- {
    this.Heapify(i)
  }
}

func (this *Heap) Heapify(pos int) {
  val, nextpos := this.heap[pos], pos * 2 + 1
  size := len(this.heap)
  for ; nextpos < size; nextpos = nextpos * 2 + 1 {
    if nextpos < size - 1 && this.heap[nextpos] > this.heap[nextpos + 1] {
      nextpos = nextpos + 1
    }
    if val > this.heap[nextpos] {
      this.heap[pos] = this.heap[nextpos]
      pos = nextpos
    } else {
      break
    }
  }
  this.heap[pos] = val
}

func (this *Heap) Push(val int) {
  this.heap = append(this.heap, val)
  pos := len(this.heap) - 1
  
  for pos > 0 {
    if this.heap[(pos - 1) / 2] > this.heap[pos] {
      this.heap[pos] = this.heap[(pos - 1) / 2]
      pos = (pos - 1) / 2
    } else {
      break
    }
  }

  this.heap[pos] = val
}

func (this *Heap) Pop() int {
  ret := this.heap[0]
  this.heap[0] = this.heap[len(this.heap) - 1]
  this.Heapify(0)
  this.heap = this.heap[: len(this.heap) - 1]
  
  return ret
}

func (this *Heap) Empty() bool {
  return len(this.heap) == 0
}

func sortArray(nums []int) []int {
  heap_ := Heap{}
  heap_.Construct(nums)

  ret := make([]int, 0)
  for heap_.Empty() == false {
    ret = append(ret, heap_.Pop())
  }
  return ret
}

代码实现(九刷自解 DAY 12 golang)


func mergeSort(nums, tmp []int, left, right int) {
  if left >= right {
    return
  }

  mid := ((right - left) >> 1) + left
  mergeSort(nums, tmp, left, mid)
  mergeSort(nums, tmp, mid + 1, right)

  l, r, pos := left, mid + 1, left
  for l <= mid && r <= right {
    if nums[l] <= nums[r] {
      tmp[pos] = nums[l]
      l++
    } else {
      tmp[pos] = nums[r]
      r++
    }
    pos++
  }
  for l <= mid { 
    tmp[pos] = nums[l] 
    pos, l = pos + 1, l + 1
  }
  for r <= right { 
    tmp[pos] = nums[r] 
    pos, r = pos + 1, r + 1
  }

  for i := left; i <= right; i++ {
    nums[i] = tmp[i]
  }
}

func sortArray(nums []int) []int {
  tmp := make([]int, len(nums))
  mergeSort(nums, tmp, 0, len(tmp) - 1)
  
  return nums
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Love 6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值