Created
November 29, 2023 14:51
-
-
Save adeel-raza/a74f4867fcd9140a0709974e3f60aa2c to your computer and use it in GitHub Desktop.
program to sort a list of contacts with Bubble sort
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
<?php | |
$contact_info = array( | |
'Zeeshan ' => ' 03332490820', | |
'Bilal ' => ' 03332490895', | |
' Danish ' => ' 03332490673', | |
'Sami' => ' 03332490165', | |
'Ahmed' => ' 03332490090', | |
' Batool ' => ' 03332490554', | |
'Owais ' => ' 03332490967', | |
'Ahsan ' => ' 03332490534', | |
'Ehsan' => ' 03332490899', | |
' Imtiaz ' => '03332490678', | |
); | |
$sorted_array = array(); | |
function sort_contact_info( $contact_info ) { | |
$no_of_contacts = count( $contact_info ) - 1; | |
for ( $step = 0; $step < $no_of_contacts; $step++ ) { | |
for ( $position = 0; $position < $no_of_contacts - $step; $position++ ) { | |
if ( $contact_info[ $position ] > $contact_info[ $position + 1 ] ) { | |
$tmp = $contact_info[ $position + 1 ]; | |
$contact_info[ $position + 1 ] = $contact_info[ $position ]; | |
$contact_info[ $position ] = $tmp; | |
} | |
} | |
} | |
return $contact_info; | |
} | |
// Remove whitespace from array keys and values | |
foreach ( $contact_info as $name => $contact_no ) { | |
$name = trim( $name ); | |
$contact_no = trim( $contact_no ); | |
$formatted_contact_info[ $name ] = $contact_no; | |
} | |
$contact_names = array_keys( $formatted_contact_info ); | |
$sorted_contact_names = sort_contact_info( $contact_names ); | |
foreach ( $sorted_contact_names as $key => $name ) { | |
unset( $sorted_contact_names[ $key ] ); | |
$sorted_contact_names[ $name ] = $formatted_contact_info[ $name ]; | |
} | |
?> | |
<h3> Sorted Contact List </h3> | |
<?php foreach ( $sorted_contact_names as $name => $contact_no ) : ?> | |
<p> | |
<?php echo $name . ' : ' . $contact_no; ?> | |
</p> | |
<?php | |
endforeach; | |
function sort_contacts_by_name( $contact_names ) { | |
$number_of_contacts = count( $contact_names ); | |
for ( $pass_number = 0; $pass_number < $number_of_contacts - 1; $pass_number++ ) { | |
for ( $position = 0; $position < $number_of_contacts - $pass_number - 1; $position++ ) { | |
// Use strcasecmp for case-insensitive comparison | |
$name_1 = $contact_names[ $position ]; | |
$name_2 = $contact_names[ $position + 1 ]; | |
if ( strcasecmp( $name_1, $name_2 ) > 0 ) { | |
//if ( $name_1 > $name_2 ) { | |
// Swap values at position $position and $position + 1 | |
$temp_name = $contact_names[ $position ]; | |
$contact_names[ $position ] = $contact_names[ $position + 1 ]; | |
$contact_names[ $position + 1 ] = $temp_name; | |
} | |
} | |
} | |
return $contact_names; | |
} | |
// Dummy contact list | |
$contacts = array( | |
'Zeeshan ' => ' 03332490820', | |
'Bilal ' => ' 03332490895', | |
' Danish ' => ' 03332490673', | |
'Sami' => ' 03332490165', | |
'Ahmed' => ' 03332490090', | |
' Batool ' => ' 03332490554', | |
'Owais ' => ' 03332490967', | |
'Ahsan ' => ' 03332490534', | |
'Ehsan' => ' 03332490899', | |
' Imtiaz ' => '03332490678', | |
'aziz' => '03332490671', | |
); | |
// Format contacts and remove extra spaces | |
foreach ( $contacts as $contact_name => $contact_no ) { | |
$contacts_formatted[ trim( $contact_name ) ] = trim( $contact_no ); | |
} | |
$contacts = $contacts_formatted; | |
// Sort the contact list using bubble sort | |
$sorted_names = sort_contacts_by_name( array_keys( $contacts ) ); | |
// Create a new array with sorted keys and corresponding values | |
$sorted_contacts = array(); | |
foreach ( $sorted_names as $name ) { | |
$sorted_contacts[ $name ] = $contacts[ $name ]; | |
} | |
?> | |
<h3>Sorted Contact List:</h3> | |
<br /> | |
<?php foreach ( $sorted_contacts as $name => $contact_no ) : ?> | |
<?php | |
// Print the sorted list | |
echo $name . " => $contact_no <br />"; | |
?> | |
<?php endforeach ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment