当前位置: 编程技术>移动开发
本页文章导读:
▪排序算法-取舍排序(简单插入排序、堆排序) 排序算法---选择排序(简单插入排序、堆排序)
#include <stdio.h>
void simple_select(int a[], int n)
{
int i,j,k,tmp;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
.........
▪ 排序算法-交换排序(冒泡排序、快速排序) 排序算法---交换排序(冒泡排序、快速排序)
#include <stdio.h>
void bubble_sort(int a[], int n)
{
int i,j,tmp;
for(i=0;i<n-1;i++)
for(j=0;j>n-1-i;j++)
if(a[j]>a[j+1].........
▪ 可舒卷的表视图 可伸缩的表视图
头文件:
#import <UIKit/UIKit.h>
@interface TableMenuViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *itemArray;
NSMutableArray *openItemArray;
UITableView *m.........
[1]排序算法-取舍排序(简单插入排序、堆排序)
来源: 互联网 发布时间: 2014-02-18
排序算法---选择排序(简单插入排序、堆排序)
#include <stdio.h>
void simple_select(int a[], int n)
{
int i,j,k,tmp;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[k])
k=j;
}
if(k!=i)
{
tmp=a[k];
a[k]=a[i];
a[i]=tmp;
}
}
}
void shift(int a[], int low, int high)
{
int i=low;
int j=2*i;
int tmp=a[i];
while(j<=high)
{
if(j<high && a[j]<a[j+1])
j++;
if(tmp<a[j])
{
a[i]=a[j];
i=j;
j=2*i;
}
else
break;
}
a[i]=tmp;
}
/*the number of elements of a is n+1*/
/*a[0] is not used*/
void heap_sort(int a[], int n)
{
int i,tmp;
for(i=n/2;i>=1;i--)
shift(a,i,n);
for(i=n;i>=2;i--)
{
tmp=a[i];
a[i]=a[1];
a[1]=tmp;
shift(a,1,n-1);
}
}
[2] 排序算法-交换排序(冒泡排序、快速排序)
来源: 互联网 发布时间: 2014-02-18
排序算法---交换排序(冒泡排序、快速排序)
#include <stdio.h>
void bubble_sort(int a[], int n)
{
int i,j,tmp;
for(i=0;i<n-1;i++)
for(j=0;j>n-1-i;j++)
if(a[j]>a[j+1])
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
int partition(int a[], int low, int high)
{
int pivot=a[low];
while(low<high)
{
while(low<high && a[high]>=pivot)--high;
a[low]=a[high];
while(low<high && a[low]<=pivot)++low;
a[high]=a[low];
}
a[low]=pivot;
return low;
}
void qsort(int a[], int low, int high)
{
int pivot;
if(low<high)
{
pivot=partition(a,low,high);
partition(a,low,pivot-1);
partition(a,mid+1,high);
}
}
void quick_sort(int a[], int n)
{
qsort(a,0,n-1);
}
[3] 可舒卷的表视图
来源: 互联网 发布时间: 2014-02-18
可伸缩的表视图
头文件:
#import <UIKit/UIKit.h>
@interface TableMenuViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *itemArray;
NSMutableArray *openItemArray;
UITableView *menuTable;
}
@property (nonatomic, retain) NSMutableArray *itemArray;
@property (nonatomic, retain) NSMutableArray *openItemArray;
@property (nonatomic, retain) IBOutlet UITableView *menuTable;
- (void)readPlistToArray;
@end
实现文件:
#import "TableMenuViewController.h"
@implementation TableMenuViewController
@synthesize itemArray, openItemArray, menuTable;
- (void)viewDidLoad {
[super viewDidLoad];
self.itemArray = [NSMutableArray arrayWithCapacity:0];
self.openItemArray = [NSMutableArray arrayWithCapacity:0];
[self readPlistToArray];
}
- (void)readPlistToArray {
if ([self.itemArray count]) {
[self.itemArray removeAllObjects];
}
NSString *path = [[NSBundle mainBundle] pathForResource:@"MenuOrder" ofType:@"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:path];
for (int i = 0; i < [array count]; i++) {
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
[dic setObject:@"0" forKey:@"level"];
[dic setObject:[[array objectAtIndex:i] objectAtIndex:0] forKey:@"name"];
[self.itemArray addObject:dic];
for (int j = 0; j < [self.openItemArray count]; j++) {
if ([[[self.openItemArray objectAtIndex:j] objectForKey:@"name"]
isEqualToString:[[array objectAtIndex:i] objectAtIndex:0]]) {
NSArray *ary = [array objectAtIndex:i];
for (int k = 1; k < [ary count]; k++) {
NSMutableDictionary *menuDic = [NSMutableDictionary dictionaryWithCapacity:0];
[menuDic setObject:@"2" forKey:@"level"];
[menuDic setObject:[ary objectAtIndex:k] forKey:@"name"];
[self.itemArray addObject:menuDic];
}
}
}
}
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.itemArray count];
}
- (NSInteger)tableView:(UITableView *)tableView
indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [[[self.itemArray objectAtIndex:[indexPath row]] objectForKey:@"level"] intValue];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSDictionary *dic = [self.itemArray objectAtIndex:[indexPath row]];
if(![[dic objectForKey:@"level"] intValue])
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"menubackgroud.png"]];
[cell setBackgroundView:imageView];
}
else {
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"itembackgroud.png"]];
[cell setBackgroundView:imageView];
}
cell.textLabel.text = [dic objectForKey:@"name"];
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *dic = [self.itemArray objectAtIndex:[indexPath row]];
NSString *name = [dic objectForKey:@"name"];
if(![[dic objectForKey:@"level"] intValue])
{
for (int i = 0; i < [self.openItemArray count]; i++) {
if ([[[self.openItemArray objectAtIndex:i] objectForKey:@"name"] isEqualToString:name]) {
[self.openItemArray removeObjectAtIndex:i];
[self readPlistToArray];
[self.menuTable reloadData];
return;
}
}
[self.openItemArray addObject:dic];
[self readPlistToArray];
[self.menuTable reloadData];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:(@"%@", name) delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44;
}
- (void)dealloc {
[itemArray release];
[openItemArray release];
[menuTable release];
[super dealloc];
}
@end
示例图:
最新技术文章: