040_swift_图形化实现冒泡排序

时间:2020-12-17 00:53:15


效果展示

​​下载地址​​

//
// SortView.swift
// DemoApp
//
// Created by liuan on 2020/5/25.
// Copyright © 2020 www.liuan.com. All rights reserved.
//冒泡算法
// 循环比较需排序的数列,如果他们的顺序错误就替换过来。重复这个操作,直到所有数组完成这个排列


//

import UIKit

class SortView: UIView {

/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
override init(frame: CGRect) {
super.init(frame: frame)
self.frame = frame
}
required init?(coder: NSCoder) {
fatalError("init has not been imlemented")
}

override func layoutSubviews() {
let y:CGFloat = 300 - frame.height
self.frame = frame
self.frame.origin.y = y
// 根据自定义视图的高度。计算一个权重数值,用于沈城不同的背景颜色

let weight = frame.height/200
let color = UIColor(hue: weight, saturation: 1, brightness: 1, alpha: 1)
self.backgroundColor = color
}


}
//
// ViewController.swift
// sort
//
// Created by liuan on 2020/5/25.
// Copyright © 2020 liuan. All rights reserved.
//

import UIKit
extension Array{
//交换位置 mutating为了修改数值
fileprivate mutating func swap(i:Int,j:Int){
let temp = self[i]
self[i] = self[j]
self[j] = temp
}
}
//对具有可比较性的数组,进行扩展。实现冒泡排序功能
extension Array where Element:Comparable{
mutating func bubbleSort(){
//从第一个元素 到倒数第二个元素
for i in 0..<self.count-1
{
// 从数组的尾部开始,向上一个循环语句遍历到的元素进行遍历
for j in (i+1...self.count-1).reversed(){
if self[j] < self[j-1]{
swap(i: j, j: j-1)
}
}

}
}
}


class ViewController: UIViewController {
var result:Array<Int> = Array<Int>()
var date : Date!
var temView:Array<SortView> = Array<SortView>()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
initTempDate()
let bt = UIButton(frame: CGRect(x: 10, y: 340, width: 400, height: 40))
bt.backgroundColor = .orange
bt.setTitle("冒泡排序", for: .normal)
bt.addTarget(self, action: #selector(reOrderView), for: .touchUpInside)
self.view.addSubview(bt)

let bt2 = UIButton(frame: CGRect(x: 10, y: 400, width: 400, height: 40))
bt2.backgroundColor = .orange
bt2.setTitle("复原", for: .normal)
bt2.addTarget(self, action: #selector(reBegin), for: .touchUpInside)
self.view.addSubview(bt2)
}
@objc func reOrderView(){
date=Date()
// 在一个全局队列中,以异步的当时对数组进行排序。并实时调整和数组中的数组值向对应的视图的位置

DispatchQueue.global().async {
for i in 0..<self.result.count-1{
for j in (i+1...self.result.count-1).reversed(){
if(self.result[j]<self.result[j-1]){
weak var weak_self = self

//修改界面要在主线程
DispatchQueue.main.async {
let view1 = weak_self?.view.viewWithTag(j+1)
let view2 = weak_self?.view.viewWithTag(j)
let posX1 = view1?.frame.origin.x
let posX2=view2?.frame.origin.x

view1?.frame.origin.x=posX2!
view2?.frame.origin.x=posX1!

view1?.tag=j
view2?.tag=j+1
self.result.swap(i: j, j: j-1)

}
Thread.sleep(forTimeInterval: 0.01)
}

}
}
}

let endDate=Date()
print(endDate.timeIntervalSince(self.date))
}
func initTempDate(){
var array:Array<Int> = Array<Int>()
for i in 1...100
{
array.append(i)
}
result.removeAll()
for _ in 1...100{
//根据数组的长度,获得一个1至100的随机数
let temp = Int(arc4random() % UInt32(array.count))+1
let num = array[temp-1]
result.append(num)
//从原来数组中移出该元素。防止重复
array.remove(at: temp-1)
}
temView.removeAll()
for i in 1...100{
let num = result[i-1]
let view = SortView(frame: CGRect(x: 10+i*3, y: 200, width: 2, height: num*2))
view.backgroundColor = .black
view.tag = i
temView.append(view)
self.view.addSubview(view)

}
}
@objc func reBegin(){
for x in temView{
self.view.viewWithTag(x.tag)?.removeFromSuperview()
}
initTempDate()
}



}