Skip to content

Instantly share code, notes, and snippets.

@mamaz
mamaz / gist:6099633
Created July 28, 2013 18:48
Penjelasan Simple Merge Sort
Merge Sort
Merge sort sebenarnya ya sorting mengurutkan dari kecil ke besar dari besar ke kecil, implementasinya yg disorting itu bisa angka (int, float atau double), bisa juga karakter ASCII atau Unicode.
Bedanya sorting di sini menggunakan prinsip divide and conquer atau devide et impera :D
Untuk memahami merge sort, perlu pemahaman tentang fungsi rekursif, kalo belom tahu ya belajar dulu tentang rekursif.
Ada tiga step penting di merge sort, yaitu:
1. Sort setengah bagian kiri dari array. (rekursif)
[FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
FBRequest *met = [FBRequest requestWithGraphPath:@"/me/feed" parameters:nil HTTPMethod:@"GET"];
[met startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
// dapetin result
}];
}];
+(CGSize)getLabelSizeWordWrapWithString:(NSString*)string font:(UIFont*)font
constrainedToSize:(CGSize)constrainedSize
{
if (IOS_VERSION_LESS_THAN_7) {
CGSize size = [string sizeWithFont:font
constrainedToSize:constrainedSize
lineBreakMode:NSLineBreakByWordWrapping];
return size;
// Create a generated tiny UIImage with color
// Created not by me.
- (UIImage *)imageWithColor:(UIColor *)color
{
CGRect rect = CGRectMake(0, 0, 1, 1);
// Create a 1 by 1 pixel context
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
[color setFill];
UIRectFill(rect); // Fill it with your color
// Get string time duration from 2 different NSDate object
- (NSString *)getNaturalLanguageForDate:(NSDate *)date1 andDate:(NSDate *)date2 withCalendar:(NSCalendar *)sysCalendar
{
// Get conversion to months, days, hours, minutes
unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;
NSDateComponents *breakdownInfo = [sysCalendar components:unitFlags fromDate:date1 toDate:date2 options:0];
@mamaz
mamaz / ScaledToFillSize.m
Created June 24, 2014 07:12
ScaledToFillSize
// Return image Image scaledToFillSize, perfect for thumbnailing
// taken from http://stackoverflow.com/a/17884863/1328982
+ (UIImage *)imageWithImage:(UIImage *)image scaledToFillSize:(CGSize)size
{
CGFloat scale = MAX(size.width/image.size.width, size.height/image.size.height);
CGFloat width = image.size.width * scale;
CGFloat height = image.size.height * scale;
CGRect imageRect = CGRectMake((size.width - width)/2.0f,
(size.height - height)/2.0f,
width,
@mamaz
mamaz / NSLogExtension
Last active August 29, 2015 14:05
Print filename and line with comments
// Print filename and line with comments
// credits to Taufik Obet
#ifndef __OPTIMIZE__
# define NSLog(...) printf("[%-30s:%4d]: %s\n", __FILE_NAME_ONLY__, __LINE__, [[NSString stringWithFormat:__VA_ARGS__] UTF8String])
#else
#define NSLog(...) do {} while (0)
#endif
/*
Get UILabel's size for wordwrapping style
*/
+(CGSize)getLabelSizeWordWrapWithString:(NSString*)string
font:(UIFont*)font
constrainedToSize:(CGSize)constrainedSize
{
if (IOS_VERSION_LESS_THAN_7) {
@mamaz
mamaz / MZCircleImage.m
Created September 11, 2014 10:06
Draw Circle Image by Clipping
// draw circler image
-(void)drawClippedAvatar:(UIImage *)image inRect:(CGRect)rect andContext:(CGContextRef)context
{
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddEllipseInRect(path, NULL, rect);
CGContextAddPath(context, path);
CGContextClip(context);
CGPathRelease(path);
[image drawInRect:rect];
}
@mamaz
mamaz / initString.cpp
Last active August 29, 2015 14:07
initString.cpp simple pass by reference
#include <iostream>
using namespace std;
/**
User refrence sign '&' to make it pass by refernce.
*/
void initString(char* &dest) {
dest = new char[255];
dest[0] = 'a';