Implementation of basic sorting algorithm

Source: Internet
Author: User

1. Bubbling:

#include <stdio.h>
/* Bubble Sort Algorithm implementation */
void println (int array[], int len)
{
int i = 0;

for (i=0; i<len; i++)
{
printf ("%d", array[i]);
}

printf ("\ n");
}

void swap (int array[], int i, int j)
{
int temp = Array[i];

Array[i] = Array[j];

ARRAY[J] = temp;
}
The bubbling sort is as follows
void Bubblesort (int array[], int len)//O (N*n)
{
int i = 0;
int j = 0;
int exchange = 1;
Large number precipitate fractional float
for (i=0; (I<len) && exchange; i++)
{
Indicates whether an exchange optimization occurred
Exchange = 0;

for (j=len-1; j>i; j--)
{
if (Array[j] < array[j-1])
{
Swap (Array, j, j-1);

Exchange = 1;
}
}
}
}

int main ()
{
int array[] = {21, 25, 49, 25, 16, 8};
int len = sizeof (array)/sizeof (*array);

println (array, len);

Bubblesort (array, len);

println (array, len);

return 0;
}
2. Select:

#include <stdio.h>
Sort results Print
void println (int array[], int len)
{
int i = 0;

for (i=0; i<len; i++)
{
printf ("%d", array[i]);
}

printf ("\ n");
}
Exchange of data
void swap (int array[], int i, int j)
{
int temp = Array[i];

Array[i] = Array[j];

ARRAY[J] = temp;
}
Select Sort as follows
void Selectionsort (int array[], int len)//O (N*n)
{
int i = 0;
int j = 0;
int k =-1;

for (i=0; i<len; i++)
{
Temp variable
K = i;

for (j=i; j<len; j + +)
{
if (Array[j] < array[k])
{
K = J;
}
}
Data exchange
Swap (array, I, K);
}
}

int main ()
{
Initializing data
int array[] = {21, 25, 49, 25, 16, 8};
int len = sizeof (array)/sizeof (*array);

println (array, len);

Selectionsort (array, len);

println (array, len);

return 0;
}

3. Insert:

#include <stdio.h>
/* Insert Sort implementation */
void println (int array[], int len)
{
int i = 0;

for (i=0; i<len; i++)
{
printf ("%d", array[i]);
}

printf ("\ n");
}

void swap (int array[], int i, int j)
{
int temp = Array[i];

Array[i] = Array[j];

ARRAY[J] = temp;
}
Implementing an Insert Sort
void Inertionsort (int array[], int len)//O (N*n)
{
int i = 0;
int j = 0;
int k =-1;
int temp =-1;

for (I=1; i<len; i++)
{
K = i;
temp = Array[k];
Inserts data into a set of numbers that are already sequenced (an element can also be looked at in order)
for (j=i-1; (j>=0) && (array[j]>temp); j--)//find ordered sequence to compare temp represents the number currently inserted
{
ARRAY[J+1] = Array[j];
K = J;
}

ARRAY[K] = temp;
}
}

int main ()
{
int array[] = {21, 25, 49, 25, 16, 8};
int len = sizeof (array)/sizeof (*array);

println (array, len);

Inertionsort (array, len);

println (array, len);

return 0;
}

Implementation of basic sorting algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.