sort

【LeetCode 33】Search in Rotated Sorted Array

拜拜、爱过 提交于 2020-01-17 13:30:46
题意: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplicate exists in the array. Your algorithm’s runtime complexity must be in the order of O(log n). 思路: 想到了二分,但没想到,关键是,可以根据mid和nums[l]和nums[r]的大小关系,判断出一个单调区间,然后根据target和这个区间两端值得大小关系,判断是否在这一边,决定保留哪一边。 代码: class Solution { public : int search ( vector < int > & nums , int target ) { int l = 0 ; int r = nums . size ( ) - 1 ; if

C语言利用qsort()函数实现排序

血红的双手。 提交于 2020-01-17 08:56:01
引用#include<stdlib.h>头文件 qsort()括号里面有4个参数 第一个参数是将要排序的数组名array; 第二个参数是将要排序的数量n; 第三个参数是每个要排序的参数的大小xizeof(array[o]); 第四个参数是自己写的一个比较函数comp; 若排序的是int类型的数组 升序: int comp(const void*a,const void*b)//用来做比较的函数。 { return *(int*)a - *(int*)b; } 降序: int comp(const void*a,const void*b)//用来做比较的函数。 { return *(int*)b - *(int*)a; //降序 } 若排序的是结构体数组,假定结构体node typedef struct{ int a; int b; }node; qsort(a,n,sizeof(node),comp) int comp( const void*x, const void*y) { return ((*(node*)y).a) - ((*(node*))x).a); } 来源: https://www.cnblogs.com/Hqx-curiosity/p/12204085.html

Sorts

北城以北 提交于 2020-01-17 02:31:05
See this ariticle on my own blog https://dyingdown.github.io/2020/01/16/Sorts/ Here are some sorting ways and I use many data to test and compare. Just to illustrate the property of each algorithm. Insertion Sort // // Insertion_Sort // # include <bits/stdc++.h> using namespace std ; typedef long long ll ; const int MAXN = 1e8 + 10 ; ll compare = 0 ; ll insert_sort ( int num [ ] , int len ) { ll times = 0 ; for ( int i = 1 ; i < len ; i ++ ) { int key = num [ i ] ; int j = i - 1 ; while ( j >= 0 && key < num [ j ] ) { compare ++ ; // cout << compare << endl; num [ j + 1 ] = num [ j ] ; j -- ;

7-22 字符串排序 (20分)(sort)

本小妞迷上赌 提交于 2020-01-16 23:59:18
本题要求编写程序,读入5个字符串,按由小到大的顺序输出。 输入格式: 输入为由空格分隔的5个非空字符串,每个字符串不包括空格、制表符、换行符等空白字符,长度小于80。 输出格式: 按照以下格式输出排序后的结果: After sorted: 每行一个字符串 输入样例: red yellow blue green white 输出样例: After sorted: blue green red white yellow 在这里插入代码片#include < bits / stdc ++ . h > using namespace std ; struct str { string t ; } s [ 5 ] ; bool cmp ( str & a , str & b ) { return a . t < b . t ; } int main ( ) { // freopen("D:\\LYJ.txt","r",stdin); for ( int i = 0 ; i < 5 ; i ++ ) { cin >> s [ i ] . t ; } sort ( s , s + 5 , cmp ) ; cout << "After sorted:" << endl ; for ( int i = 0 ; i < 5 ; i ++ ) { cout << s [ i ] . t << endl

Spring Data - Spring Data JPA 提供的各种Repository接口

瘦欲@ 提交于 2020-01-16 18:34:39
Spring Data Jpa 最近博主越来越懒了,深知这样不行。还是决定努力奋斗,如此一来,就有了一下一波复习 演示代码都基于Spring Boot + Spring Data JPA 传送门: 博主的 测试代码 ------------------------------------------------------------------------------------------------------------------------------ 什么是Spring Data JPA? Spring Data 是Spring提供的操作数据的框架在Spring data JPA是Spring data的一个模块,通过Spring data 基于jpa标准操作数据的模块。 Spring Data的核心能力,就是基于JPA操作数据,并且可以简化操作持久层的代码。 Spring Data JPA提供的核心接口 前提数据: -- MySQL dump 10.13 Distrib 5.6.16, for debian-linux-gnu (x86_64) -- -- Host: localhost Database: amber -- ------------------------------------------------------ -- Server

拓扑排序(topological sort)

﹥>﹥吖頭↗ 提交于 2020-01-16 13:29:33
一、定义: 对一个 有向无环图 (Directed Acyclic Graph简称DAG) G进行拓扑排序 是将G中所有顶点排成一个线性序列 使得图中任意一对顶点u和v 若边(u,v)∈E(G) 则u在线性序列中出现在v之前 注意: 有时候,这里的排序不是唯一的 二、算法 O(V+E) 有两种:入度表、dfs (一)、入度表 O(V+E): 找出图中0入度的点 依次在图中删除这些点 于是再找删掉点之后的0入度的点 然后再删除...再找点 入度为0的点 用 队列 图 用 邻接表 #include<cstdio> #include<algorithm> #include<queue> using namespace std; const int maxn = 10005; int n,m,cnt;//cnt存储ans数组的下标 bool a[maxn][maxn];//邻接矩阵 int edge[maxn],ans[maxn];//edge[i]第i个点的入度,ans答案队列 queue<int> q; void topo_sort() { for(int i = 1;i <= n;i++) if(!edge[i]) q.push(i);//将所有入度为0的点加入队列中 while(!q.empty()) { const int u = q.front(); ans[++cnt] =

Python列表排序

浪尽此生 提交于 2020-01-16 04:08:03
1、冒泡排序 冒泡排序(Bubble Sort)是一种简单的排序算法。它重复地遍历要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。遍历数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。 def bubble_sort(list): n = len(list) for i in range(n - 1): for j in range( 0,n - 1 - i): if list[j] > list[j + 1]: list[j], list[j + 1] = list[j + 1], list[j] # if list[i] > list[i + 1]: # list[i], list[i + 1] = list[i + 1], list[i] print(list) list=[2,4,6,8,1,3,5,7,9] bubble_sort(list) #结果:[1,2,3,4,5,6,7,8,9] 2、插入排序 插入排序(Insertion Sort)是一种简单直观的排序算法。它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。插入排序在实现上,在从后向前的扫描过程中,需要把已排序元素逐步向后挪位,为最新元素提供插入空间。 def

7-2 单词排序(java sort快排)

…衆ロ難τιáo~ 提交于 2020-01-16 03:30:35
7-2 单词排序 (10分) 请编写程序,输入一系列单词,然后按字典顺序排序,最后输出结果。 输入格式 第一行为单词的数量 n (0<n<10000),后面有 n 行,每行一个英文单词(均由小写字母组成,且长度均不超过15个英文字母) 输出格式 共 n 行,每行一个英文单词(按字典顺序排列) 输入样例 3 stick stock stack 输出样例 stack stick stock 题意:很明确 题解:这个题卡输入输出,其实到1e5的数据java读入就很慢啦~~注意!此题就是简单快排~~~直接看代码吧! 上代码: import java.io.*; import java.util.*; public class Main { static class FastScanner{//用于快速读入大量数据 BufferedReader br; StringTokenizer st; public FastScanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in),16384); eat(""); } public void eat(String s) { st = new StringTokenizer(s); } public String nextLine() { try {

weblogic access.log日志常见分析

随声附和 提交于 2020-01-16 03:10:24
1,查看apache进程: ps aux | grep httpd | grep -v grep | wc -l // ps aux是显示所有进程和其状态。 2,查看80端口的tcp连接: netstat -tan | grep "ESTABLISHED" | grep ":80" | wc -l 3,通过日志查看当天ip连接数,过滤重复: cat access_log | grep "19/May/2011" | awk '{print $2}' | sort | uniq -c | sort -nr 4,当天ip连接数最高的ip都在干些什么(原来是蜘蛛): cat access_log | grep "19/May/2011:00" | grep "61.135.166.230" | awk '{print $7}' | sort | uniq -c | sort -nr | head -n 10 5,当天访问页面排前10的url: cat access_log | grep "19/May/2010:00" | awk '{print $7}' | sort | uniq -c | sort -nr | head -n 10 6,用tcpdump嗅探80端口的访问看看谁最高 tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F".

排序算法(Python实现)

*爱你&永不变心* 提交于 2020-01-15 07:20:03
1.冒泡排序 def bubble_sort ( lst ) : for i in range ( 1 , len ( lst ) ) : # 第n次循环找到排序后第len(lst)-n+1个位置的值【相对最大值】 for j in range ( 0 , len ( lst ) - i ) : if lst [ j ] > lst [ j + 1 ] : lst [ j + 1 ] , lst [ j ] = lst [ j ] , lst [ j + 1 ] return lst print ( bubble_sort ( [ 0 , 5 , 2 , 1 ] ) ) 2.选择排序 # 方式:找到len(lst)个相对最小值(两层for循环实现)。 def select_sort ( lst ) : for i in range ( len ( lst ) - 1 ) : # 第n次循环找到排序后第n个位置的值【相对最小值】 for j in range ( i + 1 , len ( lst ) ) : if lst [ j ] < lst [ i ] : lst [ i ] , lst [ j ] = lst [ j ] , lst [ i ] return lst print ( select_sort ( [ 0 , 5 , 2 , 1 ] ) ) 1-2