数据结构
文章平均质量分 73
小小小小小圆脸
这个作者很懒,什么都没留下…
展开
-
快速排序
//快速排序//选枢轴,左边的都比枢轴小,右边的都比枢轴大,左右两边不断向中间靠拢//不断选枢轴,递归#include <iostream>using namespace std;template <class T>void QuickSort(T *a,const int left,const int right){ if(left < ri...原创 2018-11-28 20:59:10 · 100 阅读 · 0 评论 -
挖坑法快速排序
该方法的基本思想是:1.先从数列中取出一个数作为基准数。2.分区过程,将比这个数大的数全放到它的右边,小于或等于它的数全放到它的左边。3.再对左右区间重复第二步,直到各区间只有一个数。#include <iostream>using namespace std;void QuickSort(int *array, int left, int right){ ...原创 2019-02-15 15:42:34 · 240 阅读 · 0 评论 -
java 数据结构之选择排序
思想:依次选最小的元素往前面放package Sort;import java.util.Arrays;public class SelectSort { public static void main(String[] args) { int[] arr = new int[] {45, 23, 89, 21, 12, 90, 4, 0}; ...原创 2019-04-05 15:07:50 · 122 阅读 · 0 评论 -
求两个数组的交集(去重)-Java
package LeetCode;import java.util.Arrays;import java.util.HashSet;import java.util.Set;/** * 求两个数组的交集,去掉重复好的元素 * num1 = [1, 2, 2, 1] nums2 = [2, 2] * 输出为:[2] * 解题思路:先对两个数组排序,然后两个下标同时移动,将相同的...原创 2019-04-12 18:30:35 · 1151 阅读 · 0 评论 -
求两个数组的交集(不去重) java
public static int[] intersect(int[] num1, int[] num2){ if (num1 == null) return num1; if (num2 == null) return num2; Arrays.sort(num1); Arrays.sort(num2); int i = 0, j = 0, k = 0;...原创 2019-04-12 20:18:34 · 286 阅读 · 0 评论 -
插入排序(使用模板对不用类型的数组进行排序)
//插入排序//算法:新加入元素与已经有的数据进行比较,需要移动原有数据的位置#include <iostream>using namespace std;template <class T>void Insert(const T& e, T *a, int i);//使用模版template <class T>void Inserti...原创 2018-11-27 21:04:57 · 200 阅读 · 0 评论 -
递归:排列
//利用递归进行排列组合#include <iostream>using namespace std;int c1 = 0;int c2 = 0;void show(char *p ,int m){ for(int i = 0;i<=m;i++){ cout << p[i]; };}void Permutations(...原创 2018-11-27 20:36:45 · 207 阅读 · 0 评论 -
算法:递归与迭代
//利用递归进行折半查找//迭代的折半查找#include <iostream>using namespace std;int BinarySearch_I(int *a, const int x, const int n); //迭代查找 指针a代表数组int BinarySearch_R(int *a, const int x, const int left,con...原创 2018-11-27 18:51:47 · 112 阅读 · 0 评论 -
最大堆的建立/插入/删除元素
头文件://函数声明,结构体声明//大顶堆// 下面是头文件的保护//#ifndef HEAP_MAXHEAP_H#define HEAP_MAXHEAP_Htemplate <class T>class Maxheap{public: Maxheap(int mx = 10); //构造函数,默认堆的大小为10 virtual ~Maxhea...原创 2018-11-28 23:39:04 · 760 阅读 · 0 评论 -
用栈进行插入排序的算法
//运行环境:Clion//实验数据:34 5 67 23 19 90 -1 输出结果:5 19 23 34 67 90//算法思路:先创建两个栈s1,s2,第一个存放数据,第二个辅助进行排序,通过比较栈顶元素的大小//进行出栈入栈操作,最终把排好序的元素都压如s2中,然后把s2中部的数据都压入s1中,输出s1#include <stdio.h>#include <s...原创 2018-11-29 22:50:23 · 520 阅读 · 0 评论 -
交换算法swap
//交换函数#include <iostream>using namespace std;void swap2(int a,int b); //传值的形式void swap3(int *px,int *py); //传指针的形式#define SWAP4(x,y,t) ((t)=(x),(x)=(y),(y)=(t)) //定义宏函数 全部用括号扩起来//前面几种都...原创 2018-11-27 14:15:09 · 241 阅读 · 0 评论 -
冒泡排序及其加标识优化
//从左往右扫描数据,选择最大的数据放在最右边//算法:比较相邻的两个数,如果左边的数大于右边的数就进行交换#include <iostream>using namespace std;void BubbleSort1(int list[], int n);void BubbleSort2(int a[], int n);int main() { int a[]...原创 2018-11-27 15:14:45 · 358 阅读 · 0 评论 -
选择排序算法
//选择最小的整数,放在已经排序的整数序列的左边//扫描选出最小的数的下标,标记出来,再把标记的数依次往前面排//重点:选择排序选择最小的,往左边选,一次扫描,一次交换,//与冒泡排序对比,冒泡排序在扫描过程中有很多次交换,一次扫描,最坏的情况下要交换n次#include <iostream>using namespace std;void SelectSort(int...原创 2018-11-27 15:45:34 · 124 阅读 · 0 评论 -
顺序查找
//顺序查找//没有排序的数据,只能用顺序查找//顺序查找的速度慢,100万数据,平均查找50万次#include <iostream>using namespace std;int SequentialSearch(int *a,const int n, const int x);//在数组a中查找有没有数x,数组a有n个元素int main() { int ...原创 2018-11-27 16:04:58 · 166 阅读 · 2 评论 -
二分查找
//二分查找//前提是已经排序//比如有100万个已经排序的数据(从小到大),先找到中间的数(第50万个),比较要找的数与第50万个数//假设比50万大,再把50W-100W折半为75W,再与75W比较……不断折半,2的20次方是100多万,因此最多20次就能找到#include <iostream>using namespace std;int BinarySearc...原创 2018-11-27 16:27:11 · 293 阅读 · 0 评论 -
Leetcode 143 ReorderList (带头节点) java
package AlgorithmInterview;/** * 对链表进行重新排序 * Given 1->2->3->4, reorder it to 1->4->2->3. * Given 1->2->3->4->5, reorder it to 1->5->2->4->3. * 此处为带头节点,...原创 2019-04-21 18:38:30 · 138 阅读 · 0 评论