Unity面试——算法

冒泡排序

    static int[] MaoPao(int[] arr)
    {
        for (int i = 0; i < arr.Length; i++)
        {
            for (int k = 0; k < arr.Length - i - 1; k++)
            {
                if (arr[k] > arr[k + 1]) 
                {
                    int temp = arr[k];
                    arr[k] = arr[k + 1];
                    arr[k + 1] = temp;
                }
            }
        }
        return arr;
    }

插入排序

    static int[] InsertSort(int[] arr)
    {
        for (int i = 1; i < arr.Length; i++)
        {
            //排序区最后一个数的下表
            int indexI = i - 1;
            //未排序区的第一个值
            int indexValue = arr[i];
            while (indexI >= 0 && arr[indexI] > indexValue)
            {
                arr[indexI + 1] = arr[indexI];
                indexI--;
            }
            arr[indexI + 1] = indexValue;
        }
        return arr;
    }

希尔排序

    static int[] XiErSort(int[] arr)
    {
        for (int step = arr.Length / 2; step > 0; step /= 2)
        {
            for (int i = step; i < arr.Length; i++)
            {
                int indexI = i - step;
                int indexValue = arr[i];
                while (indexI >= 0 && arr[indexI] > indexValue)
                {
                    arr[indexI + step] = arr[indexI];
                    indexI -= step;
                }
                arr[indexI + step] = indexValue;
            }
        }
        return arr;
    }

归并排序

    static int[] merge(int[] arr)
    {
        if (arr.Length < 2)
        {
            return arr;
        }
        int[] leftArr = new int[arr.Length / 2];
        int[] rightArr = new int[arr.Length - arr.Length / 2];
        for (int i = 0; i < arr.Length; i++)
        {
            if (i < arr.Length / 2)
                leftArr[i] = arr[i];
            else
                rightArr[i - arr.Length / 2] = arr[i];
        }
        return mergeSort(merge(leftArr), merge(rightArr));
    }
    static int[] mergeSort(int[] leftArr, int[] rightArr)
    {
        int[] arr = new int[leftArr.Length + rightArr.Length];
        int leftIndex = 0;//左数组索引
        int rightIndex = 0;//右数组索引
        //最终得到两个新的数组
        for (int i = 0; i < arr.Length; i++)
        {
            if (leftIndex >= leftArr.Length)
            {
                arr[i] = rightArr[rightIndex];
                rightIndex++;
            }
            else if (rightIndex >= rightArr.Length)
            {
                arr[i] = leftArr[leftIndex];
                leftIndex++;
            }
            else
            {
                if (leftArr[leftIndex] < rightArr[rightIndex])
                {
                    arr[i] = leftArr[leftIndex];
                    leftIndex++;
                }
                else
                {
                    arr[i] = rightArr[rightIndex];
                    rightIndex++;
                }
            }
        }
        return arr;
    }

快速排序

    static void QuickSort(int[] arr,int left,int right)
    {
        if (left >= right) 
            return;
        int tempLeft = left, tempRight = right, temp = arr[left];
        while (tempLeft != tempRight) 
        {
            //向右判断
            while (tempLeft < tempRight && arr[tempRight] > temp) 
            {
                tempRight--;
            }
            arr[tempLeft] = arr[tempRight];
            //向左判断
            while (tempLeft < tempRight && arr[tempLeft] < temp)
            {
                tempLeft++;
            }
            arr[tempRight] = arr[tempLeft];
        }
        arr[tempRight] = temp;
        QuickSort(arr, left, tempLeft - 1);
        QuickSort(arr, tempRight + 1, right);
    } 

堆排序

    //实现父节点的左右比较
    static void HeapCompare(int[] arr,int nowIndex,int arrLength)
    {
        int left = 2 * nowIndex + 1;
        int right = 2 * nowIndex + 2;
        int bigIndex = nowIndex;
        if (left < arrLength && arr[left] > arr[bigIndex])
        {
            bigIndex = left;
        }
        if (right < arrLength && arr[right] > arr[bigIndex])
        {
            bigIndex = right;
        }
        if (bigIndex != nowIndex)
        {
            int temp = arr[bigIndex];
            arr[bigIndex] = arr[nowIndex];
            arr[nowIndex] = temp;
            HeapCompare(arr, bigIndex, arrLength);
        }
    }
    //构建大堆顶
    static void BuildBigHeap(int[] arr)
    {
        for (int i = arr.Length / 2 - 1; i >= 0; i--) 
        {
            HeapCompare(arr, i, arr.Length);
        }
    }
    //把对顶不断后移
    static void HeapSort(int[] arr)
    {
        BuildBigHeap(arr);
        for (int i = arr.Length - 1; i > 0; i--) 
        {
            int tmp = arr[i];
            arr[i] = arr[0];
            arr[0] = tmp;
            HeapCompare(arr, 0, i);
        }
    }

深拷贝lua表

function table.deepCopy(tb)
if tb == nil then
return nil
end
local copy = {}
for k, v in pairs(tb) do
if type(v) == ‘table’ then
copy[k] = table.deepCopy(v)
else
copy[k] = v
end
end
setmetatable(copy, table.deepCopy(getmetatable(tb)))
return copy
end
t = {1, 2, 333, “jdkfj”, false, {4, 5, 6, {“hhh”, 3}, 1}, 0, “jfdks”}
copyT = table.deepCopy(t)
for k,v in pairs (copyT) do
print(v)
end

lua面向对象

–元类
people = {name = “” , age = 0}
–创建派生类
function people:new ( o,name,age )
o = o or {}
setmetatable(o,self)
self.__index = self
self.name = name
self.age = age
return o
end
–添加派生方法
function people:namePrint()
print(self.name)
end
–派生类
people1 = people:new(nil,“李白”,11)
people1:namePrint()
–重写派生方法
function people1:namePrint(aa)
print (“重写”…self.name…aa)
end
people1:namePrint(“hhh”)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值