## Ruby: Insertion Sort

```def insertion_sort(a)
for j in 1...a.length
key = a[j]
i = j - 1

while (i>=0 and a[i] > key)
a[i+1] = a[i]
i = i - 1
end
a[i+1] = key
end
a
end

array = %w{9 8 7 6 5 4 3 2 1 0}
insertion_sort(array)```

## Ruby: Selection Sort

```a = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

def selection_sort(a)
a.size.times do |i|
min = i
for j in (i+1)...a.size
if a[j] < a[min]
min = j
end
end

a[i], a[min] = a[min], a[i] if min!=i
end
a
end

selection_sort(a)```

## Ruby: Bubble Sort

```class Array
def bubble_sort
n = self.length
i = 0
loop do
swapped = false
(n-i-1).times do |i|
if self[i] > self[i+1]
self[i], self[i+1] = self[i+1], self[i]
swapped = true
end
end

break if not swapped
i = i+1
end

self
end
end
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1].bubble_sort```

## Ruby: Linear Search

```class Array
def linear_search(x)
index = -1
self.length.times do |i|
if self[i] == x
index = i
break;
end
end
index
end
end

a = [13, 23, 3, 4, 35, 6, 7, 28, 9, 10]
a.linear_search(1)```

## Ruby: Binary search

``` ```

```
class Array
def binary_search(x)
i = 0
j = self.length - 1

while (i<=j)
middle = (i+j)/2

if self[middle] == x
return middle
elsif self[middle] > x
j = middle - 1
else
self[middle] < x
i = middle + 1
end
end
return - 1
end
end

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a.binary_search(1)```

## Install Yosomite with bootable USB

Mac OS X 10.10 – Yosemite – was launched on 17 October 2014.Apple
Mac OS users who are already running Mavericks on their desktop computers are now eligible for free upgrade to OS X 10.10 – Yosemite.

Those who are facing performance issues with direct upgrade via App store or installing OS X for the first time on their Macs can follow a simple step-by-step guide to perform a clean install of Yosemite using Disk Utility.

Before we delve into the detailed process of clean installing OS X Yosemite on Macs, there are a few things that need to be taken care of.

Prerequisites

a) Ensure your Mac is compatible with OS X Yosemite. These are the supported devices:

MacBook (Late 2008 Aluminium, or Early 2009 or newer)
MacBook Pro (Mid/Late 2007 or newer)
MacBook Air (Late 2008 or newer)
Mac mini (Early 2009 or newer)
Mac Pro (Early 2008 or newer)
Xserve (Early 2009)
b) Back up important data and settings (photos, videos, music files, documents and the like) on your Mac to an external hard drive. You can do this by using the Time Machine feature in Mac OS X, or third-party software such as Carbon Copy Cloner.

c) Create a bootable USB flash drive with a storage size of at least 8GB.

d) Download OS X 10.10 Yosemite Final from the Mac App Store and save it to the Applications folder – its default location.

How to Create Bootable USB Flash Drive

First, create bootable USB flash drive. Here is how you do it, courtesy of Redmond Pie:

Plug in your USB drive into the computer and open Disk Utility (in Applications).
Select the USB drive from the left sidebar and click on Erase tab.
Choose Mac OS Extended (Journaled) in the format box and let the name be Untitled (default).
Now click on Erase button and wait until the format process completes.
Then choose Partition tab and select 1 Partition from Partition Layout dropdown menu. Click on Option and ensure GUID Partition Table is selected as the partition scheme, with its name set to Untitled.
After downloading the OS X 10.10 Yosemite instakker from the Mac App Store, just quit the installer and set the downloaded file’s name to Install OS X Yosemite.app.
Launch the Terminal app (in Applications/Utilities) and type or paste the following command in, and then hit Enter:
sudo /Applications/Install OS X Yosemite.app/Contents/Resources/createinstallmedia –volume /Volumes/Untitled –applicationpath /Applications/Install OS X Yosemite.app –nointeraction

Note: The code above has missing double dashes in the quotes. Please replace a single “-” with “- -” when you enter the above command line in the terminal.

This will create the bootable USB flash drive. This takes between 10-20 minutes. Do not interrupt the Terminal or eject the flash drive during the process. Once the process completes, you will see the message “Copy Complete. Done.”
The OS X Yosemite bootable USB flash drive will be ready for use and you can start the clean installation process.

How to Clean Install OS X Yosemite on Your Mac

Step 1: Ensure the bootable USB flash drive is plugged into your Mac and restart the computer. As soon as the start-up chime plays, press the Option key (Alt).

Step 2: Choose the USB drive on the start-up drive selection screen and hit Enter/Return on the keyboard.

Step 3: Wait until the Yosemite installer appears on screen. This could take a few seconds or minutes.

Step 4: Click on Disk Utility and then hit Continue.

Step 5: Click on Macintosh HD from the left hand-side pane and then hit Erase tab on the top-right portion of the window. Leave all the settings at their default values and then hit Erase button at the bottom right. This will wipe all files stored on your startup hard drive.

Step 6: When the erasing process is complete, return to the first screen where you selected Disk Utility. Now click on Install OS X and then hit Continue.

Step 7: Choose the Macintosh HD partition you erased in step 5 and then click Install.

If you have followed all the steps correctly, your computer should now boot right into the newly installed operating system, OS X Yosemite, and you could cherish its stunning new UI.

(Source: Redmond Pie)

## Measure time taken when the request is made through CURL command

Create new file

vim curl-format.txt

paste below format

time_namelookup: %{time_namelookup}\n
time_connect: %{time_connect}\n
time_appconnect: %{time_appconnect}\n
time_pretransfer: %{time_pretransfer}\n
time_redirect: %{time_redirect}\n
time_starttransfer: %{time_starttransfer}\n
———-\n
time_total: %{time_total}\n

Make request with -w “@curl-format.txt” option as below

curl -w “@curl-format.txt” -v -X GET http://xxxxxxxxxxxxxxxxxxxxx.xml -H “Content-Type: application/xml”

## Resuming a scp transfer

How to resume a scp transfer? Using rsync.

rsync –partial –progress -rave “ssh -i xxxxxx.pem” sadiga@hostname:PATH_TO_FILE_IN_HOST_SERVER_WHICH_WAS_COPIED .

## _default_ VirtualHost overlap on port 80, the first has precedence

Solution:

find httpd.conf file

NameVirtualHost *:80

restart apache server.

## Mysql2::Error: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE)

Recently after integrating facebook authentication with my client application I started to get this kind of weird errors.
Mysql2::Error: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE)
This was because my database was set in latin1 which comes as default in mysql. so the solution to convert the database from latin1 to utf8 we need to do these things.
Change character set and collation just below the line [mysqld] in my.cnf
[mysqld]
character_set_server = utf8
collation_server = utf8_general_ci
After saving my.cnf, you can do this in console if your database is not huge OR write a migration file to change character set of all tables as below.
ActiveRecord::Base.connection.tables.each do |table|
ActiveRecord::Base.connection.execute “ALTER TABLE #{table} CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;”
end