forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request wangzheng0822#115 from jin13417/master
add by j00322883 for merge 、quick sort。
- Loading branch information
Showing
8 changed files
with
985 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/************************************************************************* | ||
> File Name: lisklist.c | ||
> Author: jinshaohui | ||
> Mail: [email protected] | ||
> Time: 18-10-07 | ||
> Desc: | ||
************************************************************************/ | ||
#include<stdio.h> | ||
|
||
|
||
struct stlistNode | ||
{ | ||
int val; | ||
struct listNode *next; | ||
}listNode; | ||
|
||
/*反转链表*/ | ||
listNode reverseList(listNode *head) | ||
{ | ||
listNode *prev = NULL; | ||
listNode *next = NULL; | ||
|
||
while(head != NULL) | ||
{ | ||
next = head->next; | ||
head->next = prev; | ||
prev = head; | ||
head = next; | ||
} | ||
|
||
return prev; | ||
} | ||
|
||
/*判断链表是否有环*/ | ||
int hasCycle(listNode *head) | ||
{ | ||
listNode * fast = head; | ||
listNode * low = head; | ||
|
||
while(fast != NULL && fast->next != NULL) | ||
{ | ||
low = low->next; | ||
fast = fast->next->next; | ||
if (low == fast) | ||
{ | ||
return 1; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
/*合并有序链表*/ | ||
listNode *mergeTwoLists(listNode *l1,listNode *l2) | ||
{ | ||
listNode head = {0}; | ||
listNode *pRes = &head; | ||
|
||
while(1) | ||
{ | ||
if(l1 == NULL) | ||
{ | ||
pRes->next = l2; | ||
} | ||
|
||
if (l2 == NULL) | ||
{ | ||
pRes->next = l1; | ||
} | ||
|
||
if(l1->val < l2->val) | ||
{ | ||
pRes->next = l1; | ||
l1 = l1->next; | ||
} | ||
else | ||
{ | ||
pRes->next = l2; | ||
l2 = l2->next; | ||
} | ||
pRes = pRes->next; | ||
} | ||
|
||
return head; | ||
} | ||
/* | ||
*删除链表倒数第n个节点,并返回链表头节点 */ | ||
|
||
listNode * removeNthFromEnd(listNode*headi,int n) | ||
{ | ||
listNode *fast = head; | ||
listNode *prev = NULL; | ||
listNpde *next = head; | ||
int k = n; | ||
|
||
/*快指针往后移动k-1*/ | ||
while((k > 1) && (fast != NULL)) | ||
{ | ||
fast = fast->next; | ||
k--; | ||
} | ||
|
||
/*说明链表数目不足n个*/ | ||
if (fast == NULL) | ||
{ | ||
return head; | ||
} | ||
|
||
while (fast->next != NULL) | ||
{ | ||
fast = fast->next; | ||
prev = next; | ||
next = next->next; | ||
} | ||
|
||
if(prev == NULL) | ||
{ | ||
head = head->next; | ||
} | ||
else | ||
{ | ||
prev->next = prev->next->next; | ||
} | ||
|
||
return head; | ||
} | ||
/*求链表的中间节点*/ | ||
listNode *middleNode(listNode *head) | ||
{ | ||
listNode * fast = head; | ||
listNode * low = head; | ||
|
||
while(fast != NULL && fast->next != NULL) | ||
{ | ||
low = low->next; | ||
fast = fast->next->next; | ||
} | ||
|
||
return low; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/************************************************************************* | ||
> File Name: one_two_step.c | ||
> Author: jinshaohui | ||
> Mail: [email protected] | ||
> Time: 18-10-19 | ||
> Desc: | ||
************************************************************************/ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
#include<string.h> | ||
|
||
/*爬楼梯的问题,解决重复计算,采用数据保存方法*/ | ||
|
||
int helper(int n ,int *vlaue) | ||
{ | ||
|
||
if(vlaue[n] != 0) | ||
{ | ||
return vlaue[n]; | ||
} | ||
|
||
vlaue[n] = helper(n - 1,vlaue) + helper(n - 2,vlaue); | ||
|
||
return vlaue[n]; | ||
} | ||
|
||
int climbStaris(int n) | ||
{ | ||
int *vlaue = NULL; | ||
int res = 0; | ||
|
||
vlaue = (int *)malloc(sizeof(int)*(n+1)); | ||
if(vlaue == NULL) | ||
{ | ||
return -1; | ||
} | ||
|
||
memset(vlaue,0,sizeof(int)*(n + 1)); | ||
vlaue[0] = 0; | ||
vlaue[1] = 1; | ||
vlaue[2] = 2; | ||
res = helper(n,vlaue); | ||
free(vlaue); | ||
|
||
return res; | ||
} | ||
|
||
int main() | ||
{ | ||
|
||
printf("\r\nnum%d ,%d",5,climbStaris(5)); | ||
printf("\r\nnum%d ,%d",6,climbStaris(6)); | ||
printf("\r\nnum%d ,%d",7,climbStaris(7)); | ||
return 0; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/************************************************************************* | ||
> File Name: sorts_jinshaohui.c | ||
> Author: jinshaohui | ||
> Mail: [email protected] | ||
> Time: 18-10-19 | ||
> Desc: | ||
************************************************************************/ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
#include<string.h> | ||
|
||
#define SWAP(a,b) \ | ||
do{\ | ||
(a) ^= (b);\ | ||
(b) ^= (a);\ | ||
(a) ^= (b);\ | ||
}while(0) | ||
|
||
/*冒泡排序*/ | ||
void bubble_sort(int a[],int size) | ||
{ | ||
int i = 0; | ||
int j = 0; | ||
int swap_flg = 0; | ||
|
||
if (size < 1) | ||
{ | ||
return; | ||
} | ||
|
||
for (i = size - 1; i > 0; i--)/*排序的趟数*/ | ||
{ | ||
swap_flg = 0;/*每次设置交换标识为0*/ | ||
for (j = 0; j < i; j++)/*本趟排序的遍历元素个数*/ | ||
{ | ||
if (a[j] > a[j + 1]) | ||
{ | ||
SWAP(a[j],a[j+1]); | ||
swap_flg = 1; | ||
} | ||
} | ||
/*本趟数,无数据交换的话,说明已经有序,直接退出*/ | ||
if (swap_flg == 0) | ||
{ | ||
break; | ||
} | ||
} | ||
return; | ||
} | ||
|
||
/*插入排序*/ | ||
void insert_sort(int a[],int size) | ||
{ | ||
int i = 0; | ||
int j = 0; | ||
int key = 0; | ||
|
||
for (i = 1; i < size; i ++)/*需要插入的元素个数*/ | ||
{ | ||
key = a[i];/*保存插入的元素数据*/ | ||
j = i - 1; | ||
/* i 之前的元素都是有序的,找到比key小的插入到他后面, | ||
* 比key大的,需要往后挪一个位置*/ | ||
while((j >= 0) && (a[j] > key)) | ||
{ | ||
a[j + 1] = a[j]; | ||
j--; | ||
} | ||
a[j + 1] = key; | ||
} | ||
|
||
return; | ||
} | ||
/*选择排序*/ | ||
void select_sort(int a[],int size) | ||
{ | ||
int i = 0; | ||
int j = 0; | ||
int min = 0; | ||
|
||
for (i = 0; i < size - 1; i++) | ||
{ | ||
min = i; | ||
for (j = i + 1; j < size; j++) | ||
{ | ||
if (a[j] < a[min]) | ||
{ | ||
min = j; | ||
} | ||
} | ||
|
||
if (min != i) | ||
{ | ||
SWAP(a[i],a[min]); | ||
} | ||
} | ||
return; | ||
} | ||
|
||
void dump(int a[],int size) | ||
{ | ||
int i = 0; | ||
|
||
printf("\r\n"); | ||
for (i = 0; i < size; i++ ) | ||
{ | ||
printf("%d ",a[i]); | ||
} | ||
printf("\r\n"); | ||
return; | ||
} | ||
|
||
int main() | ||
{ | ||
int a[10] = {9,11,4,15,16,3,20,44,5,10}; | ||
|
||
//bubble_sort(a,sizeof(a)/sizeof(int)); | ||
//insert_sort(a,sizeof(a)/sizeof(int)); | ||
select_sort(a,sizeof(a)/sizeof(int)); | ||
|
||
dump(a,sizeof(a)/sizeof(int)); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.