Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ C language sorting instance (select, bubble, insert, binary, fast)     - How to understand Python yield keyword (Programming)

- Linux dd command make U disk boot disk (Linux)

- Getting the Linux shell expr use (Programming)

- Multi-core CPU, multi-threading and parallel computation (Linux)

- Linux / CentOS 7.0 installation and configuration under Tomcat 8.0 (Server)

- LVS + Keepalived the DR mode (Server)

- Grub2: Save Your bootloader (Linux)

- Linux compiler installation Redis (Database)

- Install Web-based monitoring tool: Linux-Dash (Server)

- Servlet life cycle code examples (Programming)

- Install Open vSwitch under CentOS 6.5 (Linux)

- Spring Data study notes -Helloworld (Programming)

- Linux use chattr and lsattr commands to manage file and directory attributes (Linux)

- CentOS / Linux restart active NIC connection paths / network configure error Error Resolution (Linux)

- shell script: MySQL monitoring service is normal (Database)

- Whisker Menu 1.4.3 Install menu Linux (Linux)

- Java Virtual Machine class loading mechanism and bytecode execution engine (Programming)

- C # asynchronous delegates (Programming)

- Ubuntu method for single-card dual-IP (Linux)

- Zabbix monitoring disk IO status (Server)

 
         
  C language sorting instance (select, bubble, insert, binary, fast)
     
  Add Date : 2018-11-21      
         
         
         
  #Include < Stdio.h>

Select sort method ///////////////////////
// A: first address array
// N: is the number of array elements
void select_sort (int * a, int n)
{
 int i, j, k, temp;
 for (i = 0; i  {
  k = i;
  for (j = i + 1; j   {
   if (a [k]> a [j])
   {
    k = j;
   }
  }
  
  if (i! = k)
  {
   temp = a [i];
   a [i] = a [k];
   a [k] = temp;
  }
 }
}

/////////////////////// Bubble sort method
void bubble_sort (int * a, int n)
{
 int i, j, temp;
 for (i = 0; i  {
  for (j = 0; j   {
   if (a [j]> a [j + 1])
   {
    temp = a [j];
    a [j] = a [j + 1];
    a [j + 1] = temp;
   }
  }
 }
}

/////////////////////// Insert Sorting
void insert_sort (int * a, int n)
{
 int i, j, temp;
 for (i = 1; i  {
  temp = a [i];
  for (j = i-1; j> = 0 && a [j]> temp; j--)
  {
   a [j + 1] = a [j]; // moved back to the previous value
  }
  // Later inserted in a [j] is; a [j + 1] = temp
 }
}

/////////////////////// Binary sort (sort Hill)
void shell_sort (int * a, int n)
{
 int i, j, flag, temp;
 int gap = n;

 while (gap> 1)
 {
  gap = gap / 2; // increment narrow, each halved (binary)
  do
  {
   flag = 0;
   
   // N-gap control is not to limit cross-border
   for (i = 0; i    {
    j = i + gap; // value before and after the interval adjacent to compare
    if (a [i]> a [j])
    {
     temp = a [i];
     a [i] = a [j];
     a [j] = temp;
     
     flag = 1;
    }
   }
   
  } While (! Flag = 0);
 }
}

/////////////////////// Quick sort
// Find Location
int find_pos (int * a, int low, int high)
{
 int val = a [low];
 
 while (low  {
  while (low = val)
  {// Move more than, less than the assignment, descending on the contrary
   high--;
  }
  a [low] = a [high];
  
  while (low   {// Move less than, greater than the assignment, descending on the contrary
   low ++;
  }
  a [high] = a [low];
 After termination of the while loop} // low and high must be equal
 
 // High can be changed to low
 a [low] = val;
 
 return low;
}

// Low: the first element index
// High: the last element index
void quick_sort (int * a, int low, int high)
{
 if (low  {
  int pos = find_pos (a, low, high);
  quick_sort (a, low, pos-1);
  quick_sort (a, pos + 1, high);
 }
}

int main (int argc, char * argv [])
{
 int a [] = {2, 10, 8, 1, 9, 7, 5, 4, 0, 6, 3};
 // The number of array elements; int n = sizeof (a) / sizeof (a [0])
 
 // Select_sort (a, n); // Select Sorting
 // Bubble_sort (a, n); // bubble sort method
 // Insert_sort (a, n); // insert Sorting
 // Shell_sort (a, n); // binary sort (sort Hill)
 quick_sort (a, 0, n-1); // quicksort
 
 int i = 0;
 for (i = 0; i  {
  printf ( "% d", a [i]);
 }
 printf ( "\ n");
 
 return 0;
}
     
         
         
         
  More:      
 
- Android first line of code study notes (Programming)
- DIY security of Linux platform (Linux)
- Oracle Enterprise Linux 64-bit install apache-tomcat-7.0.53 step (Server)
- Oracle capability i / o (stress test database server i / o performance) (Database)
- Build the first ASP.NET 5 Web project in Mac OS X Yosemite 10.10.3 (Server)
- Erase do with HTML5 Canvas and diffusion effect (Programming)
- KVM add virtual disks (Linux)
- The OpenGL ES GLFW window structures (Programming)
- Why use Docker (Programming)
- Shuffle Process Arrangement in MapReduce (Server)
- 12 Linux Process Management Commands (Linux)
- Solaris 10 nagios monitoring system (Linux)
- Using DBMS_STAT function closes mission (Database)
- Oracle Bug caused by the collection of AWR Snapshot fault (Database)
- Linux NFS FTP use (Server)
- 10 easy to use Linux utilities Recommended (Linux)
- Docker manage data (Linux)
- Linux System Getting Started Learning: compile and install ixgbe driver in Ubuntu or Debian (Linux)
- To install and configure the Jetty server and JDK under Ubuntu 14.04.2 (Server)
- Install Git on CentOS (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.