Bubble sort (Array method and pointer method)

Source: Internet
Author: User

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7D/BA/wKioL1buuAniHjaHAAAsUDpzbQI301.png "title=" 3.png " alt= "Wkiol1buuanihjahaaasudpzbqi301.png"/>

Array method:

#include <stdio.h>

#include <stdlib.h>

bubble_sort ( int Arr [", SZ )

{

int i = 0;

                  int < Span style= "FONT-SIZE:9PT;" >j = 0;

                  for < Span style= "FONT-SIZE:9PT;" > (j = 0; J < sz < Span style= "FONT-SIZE:9PT;" >//decide how many times it takes to sort out

                 {

for < Span style= "FONT-SIZE:9PT;" > (i = 0; < sz -1-j; i++)     //determines how many times a bubble needs to be compared

if ( arr [i "< [i + 1])

{

int tmp = arr [i];

arr [i "= Arr [i + 1] ;

arr[i + 1] = tmp;

}

}

}

}


int Main ()

{

int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};

                  int < Span style= "FONT-SIZE:9PT;" >i = 0;

                 bubble_sort (arr, ) Span style= "color: #0000FF; font-family: ' Times New Roman '; font-size:1px;" >sizeof < Span style= "FONT-SIZE:9PT;" > (arr)/ sizeof ( Arr[0]);

                  for < Span style= "FONT-SIZE:9PT;" > (i = 0; < sizeof ( ARR)/ sizeof ( Arr[0]); i++)

{

printf ( "%d", Arr[i]);

}

printf ( "\ n");

System ( "pause");

return 0;

}

Pointer method:

if (* (arr + i) < * (arr + i + 1)

    {

int < Span style= "FONT-SIZE:9PT;" >tmp = * ( arr + i);

arr < Span style= "FONT-SIZE:9PT;" >+ i) = * ( arr + i + 1 );

* ( arr + i + 1) = tmp;

}

It is important to note that there are differences between arrays and pointers in understanding the differences between arrays and pointers.

The upper limit of the two-tier cycle of bubble sorting method should be mastered.

Bubble sort (Array method and pointer method)

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.