Skip to content

Instantly share code, notes, and snippets.

@ruanchao
ruanchao / compel.go
Created November 21, 2012 07:27
Go-float compels
import "math"
//p为用户自定义的比较精度,比如0.00001
func IsEqual(f1,f2,p float64) bool{
return math.Fdim(f1,f2) < p
}
@ruanchao
ruanchao / intersection.c
Created September 4, 2012 03:41 — forked from shihongzhi/intersection.c
求交集
//answer to google 2011 school interview at zju
int quick_partition(int array[], int start, int end)
{
int x = array[end];
int i = start - 1;
int tmp;
for (int j=start; j<end; ++j)
{
if (array[j]<=x)
{
@ruanchao
ruanchao / safe_function.c
Created August 23, 2012 04:49
Safe-memory-functions
/*
* safe_malloc ()
*
* Call calloc () and abort if the specified amount of memory cannot be
* allocated.
*/
void *
safe_malloc(size_t size)
{
@ruanchao
ruanchao / gist:3432022
Created August 23, 2012 03:43 — forked from brandonb927/osx-for-hackers.sh
OSX for Hackers: Mountain Lion Edition
# ~/.osx — http://mths.be/osx
###############################################################################
# General UI/UX #
###############################################################################
# Set computer name (as done via System Preferences → Sharing)
scutil --set ComputerName "MacBookPro"
scutil --set HostName "MacBookPro"
scutil --set LocalHostName "MacBookPro"
@ruanchao
ruanchao / quick_sort.c
Created August 14, 2012 08:58 — forked from mbalayil/quick_sort.c
Quick Sort using recursion in C
/** Divide : Partition the array A[low....high] into two sub-arrays
* A[low....j-1] and A[j+1...high] such that each element
* of A[low....j-1] is less than or equal to A[j], which
* in turn is is less than or equal to A[j+1...high]. Compute
* the index j as part of this partitioning procedure.
* Conquer : Sort the two sub-arrays A[low....j-1] and A[j+1....high]
* by recursive calls to quicksort
**/
#include<stdio.h>
@ruanchao
ruanchao / quick.c
Created August 14, 2012 08:57 — forked from rohit-nsit08/quick.c
quick sort
//quick sort
#include<stdio.h>
void quicksort(int*arr,int left, int right);
int partition(int *arr,int left,int right);
int main()
{
int arr[6]={5,4,1,2,8,3};
int i;
@ruanchao
ruanchao / safealloc.c
Created July 20, 2012 15:02 — forked from alexpirine/safealloc.c
Safe memory allocation in C
void * safe_malloc(size_t const size)
{
void * p = malloc(size);
if (p == NULL) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
return p;
@ruanchao
ruanchao / bst.c
Created July 11, 2012 06:55
Binary search tree - C
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#ifndef _BST_H
#define _BST_H
#define END 0
#define INSERT 1
@ruanchao
ruanchao / bubbleSort.c
Created July 7, 2012 04:15
Bubble sort - C
#include <stdio.h>
void bubbleSort(int arr[], int count)
{
int i = count, j;
int temp;
while(i > 0)
{
for(j = 0; j < i - 1; j++)
@ruanchao
ruanchao / queue.c
Created July 3, 2012 11:30
Doubly Linked list C
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#ifndef QUEUE_H
#define INPUT_BUFFER_SIZE 100
#define OP_QUEUE_ENQUEUE 0
#define OP_QUEUE_DEQUEUE 1