Categories

Archives

Syndication


Sorting methods revealed

25
Sep
Posted at 4:54 pm in Programming

There are plenty of sorting methods available, but as we all know, the king of them all is Quick Sort.

It’s kind of hard to understand the differences in time it takes to sort a number of elements, but I’ve seen this nice page that has visually shown the amount of time needed to sort with some sorting methods.

All “famous” methods are shown, like the most original “Bubble sort” which all programmers have used atleast once (atleast most of them).
Here is the function they use for bubble sort with Java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
 
class BubbleSort2Algorithm extends SortAlgorithm {
    void sort(int a[]) throws Exception {
	for (int i = a.length; --i>=0; ) {
            boolean flipped = false;
	    for (int j = 0; j<i; j++) {
		if (stopRequested) {
		    return;
		}
		if (a[j] > a[j+1]) {
		    int T = a[j];
		    a[j] = a[j+1];
		    a[j+1] = T;
		    flipped = true;
		}
		pause(i,j);
 
	    }
	    if (!flipped) {
	        return;
	    }
        }
    }
}

To give the function a challange, here is their fastest function of Quick Sort, called Fast Quick Sort.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved. 
public class FastQSortAlgorithm extends SortAlgorithm 
{
	/** This is a generic version of C.A.R Hoare's Quick Sort 
	* algorithm.  This will handle arrays that are already
	* sorted, and arrays with duplicate keys.<BR>
	*
	* If you think of a one dimensional array as going from
	* the lowest index on the left to the highest index on the right
	* then the parameters to this function are lowest index or
	* left and highest index or right.  The first time you call
	* this function it will be with the parameters 0, a.length - 1.
	*
	* @param a	   an integer array
	* @param lo0	 left boundary of array partition
	* @param hi0	 right boundary of array partition
	*/
	private void QuickSort(int a[], int l, int r) throws Exception
   {
	int M = 4;
	int i;
	int j;
	int v;
 
	if ((r-l)>M)
	{
		i = (r+l)/2;
		if (a[l]>a[i]) swap(a,l,i);	// Tri-Median Methode!
		if (a[l]>a[r]) swap(a,l,r);
		if (a[i]>a[r]) swap(a,i,r);
 
		j = r-1;
		swap(a,i,j);
		i = l;
		v = a[j];
		for(;;)
		{
			while(a[++i]<v);
			while(a[--j]>v);
			if (j<i) break;
			swap (a,i,j);
			pause(i,j);
                        if (stopRequested) {
                            return;
                        }
		}
		swap(a,i,r-1);
		pause(i);
		QuickSort(a,l,j);
		QuickSort(a,i+1,r);
	}
}
 
	private void swap(int a[], int i, int j)
	{
		int T;
		T = a[i]; 
		a[i] = a[j];
		a[j] = T;
	}
 
	private void InsertionSort(int a[], int lo0, int hi0) throws Exception
	{
		int i;
		int j;
		int v;
 
		for (i=lo0+1;i<=hi0;i++)
		{
			v = a[i];
			j=i;
			while ((j>lo0) && (a[j-1]>v))
			{
				a[j] = a[j-1];
				pause(i,j);
				j--;
			}
			a[j] = v;
	 	}
	}
 
	public void sort(int a[]) throws Exception
	{
		QuickSort(a, 0, a.length - 1);
		InsertionSort(a,0,a.length-1);
		pause(-1,-1);
	}
}

To the point:
Check this site: http://www.cs.ubc.ca/~harrison/Java/index.html

Another great resource which has the same style is http://cg.scs.carleton.ca/~morin/misc/sortalg/
On that site, you can press the images and you will see the time needed to sort with each of these functions. All functions also have a source-code for those who want to try it.

Another funny resource for sorting is this famous movie (sorting out sorting) which many computer scientists have seen:
(It’s horrible quality)

PHP spam script

22
Sep
Posted at 10:05 am in Internet, PHP, Programming

Before you comment, this is a quite lame script, however, it is quite useful as both information and other matters as well. Most people hate spamming but even though you hate it, the source code can be used as something else.

This script will spam a target site with some non existing traffic. You won’t be able to fool web analytics with this script since they will see that you’re from a single IP so there’s no reason to try.

The full source can be viewed at the bottom of this post so you don’t have to copy and extract every piece because of the comments.

Well, let’s get to the point:
In the beginning of the script, we activate error_reporting so we will get some errors written if something goes wrong:

1
2
<?php
ini_set('error_reporting', E_ALL);

After that piece, the configuration follows. Referer, spam-location and the number of hits will be written here:

3
4
5
$referer = "http://thereferersite.com"; // Where the traffic should be sent from
$spamsite = "http://www.thespamsite.com"; // The spam site location
$times = 1; // Number of times you want to traffic spam a site

To continue this, we get the header part. Here we will have the User-agent, which referer we have and which site we have targeted:

6
7
8
$header = "GET / HTTP/1.1\r\nHost: $spamurl\r\n"; // Start of the header
$header .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6\r\n"; // Which useragent. XP, English, Firefox 2.0
$header .= "Referer: $referer\r\nConnection: Close\r\n\r\n"; // Which referer to use and close the connection

When connecting with a socket you need to know which port you should pass through. PHP has a function to do this, called getservbyname. This will most likely use port 80. A full list of which service for which port is found here. We also convert the spam location site to an IP address.

9
10
$port = getservbyname('www', 'tcp'); // Get the port to use when connecting to the www using tcp.
$ip = gethostbyname($spamsite); //Gives you the IP instead of DNS

So now we have finally got to the action part. We start with a for-loop that makes this sequence the number of times we wrote in the configuration part. After that, we create a socket with AF_INET, SOCK_STREAM and SOL_TCP.
AF_INET shows that we are using a IPv4 Internet based protocol.
SOCK_STREAM gives us a reliable, sequenced full-duplex stream which the TCP protocol are based on.
SOL_TCP basically means that we use the TCP protocol.

Then we initiates a connection using socket_connect with our socket we created and the ip address of the target site along with their www port, usually port 80.

A couple of error reports later we find the code “socket_write(..” which is a function that writes the socket with the header we have written before and the length of the buffer. This is followed up by socket_close() which just closes the connection, and then by the randomization part that just makes the script sleep for some milliseconds.

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
for ($i = 1; $i <= $times; $i++) { 
  echo "Number: $i\n";
  // Create the socket!
  $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
 
  if ($socket < 0)
    echo "socket_create() failed:\n".socket_strerror($socket)."\n"; // If something fails, what?
  // Connect to site using the socket you created
  $result = socket_connect($socket, $ip, $port);
 
  if ($result < 0)
    echo "socket_connect() failed ($result):\n".socket_strerror($result)."\n";	// If something fails, what?
 
  socket_write($socket, $header, strlen($header));
 
  socket_close($socket); // Closes the socket
 
  $sleep = rand (10, 70); // For randomization
  echo "Sleeping: $sleep\n\n";
  sleep ($sleep);
}
?>

It wasn’t harder then this!
Now you can use this code to create some more funny scripts that are not lame by default (spamming is).

Remember to turn on the sockets extension in your php.ini file if you didn’t have that before. Uncomment the extension=php_sockets.dll line.

The whole source-code is here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
 
ini_set('error_reporting', E_ALL); // Give you all PHP errors reported
 
//
////Some of the configurations
//
$referer = "http://thereferersite.com"; // Where the traffic should be sent from
$spamsite = "http://www.thespamsite.com"; // The spam site location
$times = 1; // Number of times you want to traffic spam a site
 
//
//// Now we'll write the header that should be sent
//
$header = "GET / HTTP/1.1\r\nHost: $siteurl\r\n"; // Start of the header
$header .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6\r\n"; // Which useragent. XP, English, Firefox 2.0
$header .= "Referer: $referer\r\nConnection: Close\r\n\r\n"; // Which referer to use and close the connection
 
//
//// A couple of more facts before the action statrs
//
 
$port = getservbyname('www', 'tcp'); // Get the port to use when connecting to the www using tcp.
$ip = gethostbyname($spamsite); //Gives you the IP instead of DNS
 
//
////Let's spam the site with the $times number of times!
//
 
for ($i = 0; $i < $times; $i++) { 
  echo "Number: $i\n";
  // Create the socket!
  $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);// AF_INET means IPv4 Internet based protocols.
  										     // SOCK_STREAM = sequenced, reliable, full-duplex stream. The TCP protocol is based on this.
										     // SOL_TCP means that TCP will be used.
  if ($socket < 0)
    echo "socket_create() failed:\n".socket_strerror($socket)."\n"; // If something fails, what?
  // Connect to site using the socket you created
  $result = socket_connect($socket, $ip, $port);
 
  if ($result < 0)
    echo "socket_connect() failed ($result):\n".socket_strerror($result)."\n";	// If something fails, what?
 
  socket_write($socket, $header, strlen($header)); // Writes the socket, using the header we wrote earlier and the length of the buffer
  socket_close($socket); // Closes the socket
 
  $sleep = rand (10, 70); // For randomization
  echo "Sleeping: $sleep\n\n";
  sleep ($sleep);
}
?>

Alexa ranking script

15
Sep
Posted at 5:26 pm in PHP, Programming

There are plenty of people out there searching for a script to get the alexa ranking for a site. Last time I looked I didn’t even find a free function to get the data, just some expensive solutions.

So anyway, here’s a PHP coded function we have made that collects the alexa-data with the SimpleXML function. It’s very easily coded so everyone should understand what it does. However, since it uses the SimpleXML function, it can only be used if you run PHP5. That’s a requirement.

Please give us some credit if you use the function. A comment or anything will work!

1
2
3
4
5
6
7
8
9
10
<?php
 
function getalexa($url){
// cdsrc.com
$request_url = "http://data.alexa.com/data?cli=10&dat=snbamz&url=".$url;
$xml = simplexml_load_file($request_url) or die("feed not loading");
return $xml->SD->POPULARITY['TEXT'];
}
 
?>

And here’s a little description of how to use it:

11
12
13
14
<?php
echo getalexa("http://cdsrc.com");
// This would print the alexaranking of cdsrc.com
?>

Simple huh?

PHP Random password generator function

10
Sep
Posted at 11:25 am in PHP, Programming

Here’s a random password and string generator function for PHP.
It is totally free of use.

randpass(); Will give you a 8 characters long randomized string with numbers and letters.
The function also takes these arguments:
$numchars <-- The length in characters for the returned string.
$digits <------ Should the generated password contain figures? 1/0
$letters <----- Should it contain letters? 1/0
$sensitive <--- Both upper and lower letters? 1/0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function randpass($numchars=8,$digits=1,$letters=1,$sensitive=0)
{
	$dig = "12345678901234567890";
	$abc = "abcdefghijklmnopqrstuvwxyz";
	if($letters == 1)
	{
	    $str .= $abc;
	}
	if($sensitive == 1)
	{
	    $str .= strtoupper($abc);
	}
	if($digits == 1)
	{
		$str .= $dig;
	}
	for($i=0; $i < $numchars; $i++)
	{
		$randomized .= $str{rand() % strlen($str)};
	}
 
return $randomized;
}

To give an example of the code:

24
25
echo "Generated password: \n";
echo randpass(12,1,1,1);

The code above will result in:

Generated password:
4C8cly61h907